【发布时间】:2017-07-01 15:03:44
【问题描述】:
我正在编写一个脚本,该脚本通过使用套接字等的本地 HTTP 连接以编码 json 的形式将一些数据从应用程序 A 发送到应用程序 B。
应用 A 使用 python,但是在应用 B 中,您只能使用纯 JavaScript 来执行您刚刚通过 json 数据传递的操作。
这里我有一些问题,我怎样才能使用纯 JS 发送回从应用 B 内部执行的数据?它还会使用我已经在使用的端口和套接字吗?我对 JS 不是很熟悉,所以任何提示都非常适用。此外,由于这个想法的一些安全问题,我不太确定这是否可能,但也许我不知道一些事情。
app 一个python脚本伪代码:
class Connect:
def __init__(self, port=1, host='localhost'):
self._host = host
self._port = port
# Execute a HTTP POST request to the app server and send/receive JSON data
def json_post_request(self, route, body):
connection = http.HTTPConnection(self._host, self._port, timeout=3000)
connection.request('POST', route, body, head)
response = connection.getresponse()
data = json.loads(response.read().decode('utf-8'))
connection.close()
# Execute a JavaScript script
def execute_script(self, script):
return self.json_post_request(route, ('{"js":"' + script.decode('utf-8') + '"}').encode('utf-8'))
【问题讨论】:
标签: javascript python json sockets