Search code examples
brython

Getting error at Object._b_.NotImplementedError.$factory (eval at $make_exc using Brython


Hi I am using Brython as a beginner. My requirement is to ping one IP address from the browser based application. I want to know whether it is possible in case of Brython. I have written a very small piece of code to achieve it.

index.html

<html>

<head>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/brython.min.js">
    </script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/brython_stdlib.js">
    </script>
    <script type="text/python" src="./memos.py"></script>
    <script type="text/python" src="./hello.py"></script>
</head>

<body onload="brython()">

<script type="text/python">
            
            from browser import document, alert
            from browser import document
            from browser.template import Template
            from hello import sayHi, getFullName
            from memos import ping

            def pingIP(event):
                ipAddress = document["ipAdrs"].value
                pingResponse = ping(ipAddress)
                alert(pingResponse)
                document <= myFullName

            document["pingBtn"].bind("click", pingIP)
            Template(document["pingResponse"]).render(pingResponse=document)

</script>


<div id="d2">
<table>
    <tr>
        <td>Enter IP address:</td>
        <td><input type="text" name="ipAdrs" id="ipAdrs"></td>
        <td><button id="pingBtn">Ping</button></td>
        <td><span id="pingResponse">Are you able to ping (Ans: True / False): {pingResponse}</span></td>
    </tr>
</table>
</div>
</body>

</html>

Python code to ping IP address is given below

import platform  
import subprocess

def ping(host):
    """
    Returns True if host (str) responds to a ping request.
    Remember that a host may not respond to a ping (ICMP) request even if the host name is valid.
    """

    print("hiii");
    # Option for the number of packets as a function of
    param = '-n' if platform.system().lower()=='windows' else '-c'

    # Building the command. Ex: "ping -c 1 google.com"
    command = ['ping', param, '1', host]

    return subprocess.call(command) == 0

I get the following error while pinging IP address.

Uncaught Error
    at Object._b_.NotImplementedError.$factory (eval at $make_exc (brython.js:7989), <anonymous>:1:212)
    at $module.<computed> (eval at run_js (brython.js:9382), <anonymous>:128:43)
    at _execute_child13056 (eval at exec_module (brython.js:9485), <anonymous>:6024:114)
    at $ (brython.js:5492)
    at __init__13024 (eval at exec_module (brython.js:9485), <anonymous>:2801:76)
    at r.__call__ (brython.js:5698)
    at s (brython.js:5876)
    at call13011 (eval at exec_module (brython.js:9485), <anonymous>:1598:98)
    at ping9 (eval at run_py (brython.js:9435), <anonymous>:54:103)
    at pingIP0 (eval at e.loop (brython.js:5368), <anonymous>:63:93)
_b_.NotImplementedError.$factory @ VM161060:1
$module.<computed> @ VM161447:128
_execute_child13056 @ VM161128:6024
$ @ brython.js:5492
__init__13024 @ VM161128:2801
r.__call__ @ brython.js:5698
s @ brython.js:5876
call13011 @ VM161128:1598
ping9 @ VM161126:54
pingIP0 @ VM161117:63
_ @ brython.js:13593

However, I can achieve it in NodeJs.


Solution

  • This is not possible from the browser. You can not execute an system calls from a browser. Brython is like a transpiler like Typescript.

    Refer this link. As per this link "but anyway, for security reasons, it's not possible to execute a program in the command line such as ping from a browser, be it in Javascript or Brython."