首页 > 解决方案 > 如何在 Python 中从 Javascript 中获取变量

问题描述

我正在尝试制作一个从 javascript 文件中读取 JSON 字符串的程序,但我似乎做不到。我只能和我想要的相反。这是我的代码

#python 2.7?
import sys
from subprocess import Popen, PIPE

messages = [] #store messages from send.js
sensor = Popen([communication.html, send.js])
buffer = b''
while True:

    # read the sensor a character at a time
    out = sensor.stdout.read(1)

    #after reading
    if out = b'\n':
        messages.append(float(buffer))
        print(messages)
        buffer = b''
    else:
        buffer += out #append to buffer
//javascript
function sendMsg(){
    var msg = document.getElementById('talk').value;
    var data = {
        "sending": msg
    };
    data = JSON.stringify(data)
}
<!DOCTYPE html>
<html>
    <head>
        <!--<script src="comScrp.py"></script>-->
    </head>
    <body>
        <script src="send.js"></script>
        <!--Without the Python, we wouldn't be able to use this without a server or nodeJs.-->
        <input id="talk"><button onclick="sendMsg()">Send</button>
        <p id="output"></p>
    </body>
</html>

谢谢!

标签: javascriptpythonhtml

解决方案


推荐阅读