在 Object._b_.NotImplementedError.$factory 出现错误(在 $make_exc 使用 Brython 进行评估

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

您好,我正在使用 Brython 作为初学者。我的要求是从基于浏览器的应用程序 ping 一个 IP 地址。 我想知道在 Brython 的情况下是否可行。我写了一小段代码来实现。

index.html

<html>

<head>
    <script src="https://cdn.jsdelivr.net/npm/brython@3.8.10/brython.min.js">
    </script>
    <script src="https://cdn.jsdelivr.net/npm/brython@3.8.10/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 ping IP 地址的代码如下

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

我在 ping IP 地址时收到以下错误。

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

不过,我可以在NodeJs中实现。

这在浏览器中是不可能的。您不能从浏览器执行系统调用。 Brython 就像 Typescript 一样的转译器。

参考thislink。根据这个link“但是无论如何,出于安全原因,不可能在命令行中执行程序,例如从浏览器执行 ping,无论是在 Javascript 还是 Brython 中。