[Python] How to make JavaScript talk with Python through local http connection?

Discussão em 'Python' iniciado por Stack, Julho 9, 2025.

  1. Stack

    Stack Membro Participativo

    I am writing a script that sends some data from app A to app B in form of coded json through a local HTTP connection using sockets, etc.
    app A uses python, however in app B you can only use pure JavaScript to perform operations that you justed passed with json data.

    And here I have some problem, how can I send back data using pure JS that would be executed from inside app B? Also it would use port and socket, that I am already using? I am not very familiar with JS so any hints are very appriciated. Also I am not very sure that it's possible due to some security issues from this idea however maybe I am not aware of something.


    app A python script pseudocode:

    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'))

    Continue reading...

Compartilhe esta Página