I’m working on a project where I need to send information between two applications using HTTP requests on my local machine. The first application runs Python code and needs to communicate with the second application that only supports vanilla JavaScript.
My setup involves sending JSON formatted data from the Python app to the JavaScript app through HTTP connections. The tricky part is figuring out how to send data back from the JavaScript side to Python using the same connection setup.
I’m not super experienced with JavaScript networking so I’m looking for guidance on how to implement this bidirectional communication. Are there any security concerns I should be aware of when doing this kind of local HTTP communication?
Here’s what my Python connection handler looks like:
class HTTPClient:
def __init__(self, server_port=8080, server_host='127.0.0.1'):
self.host = server_host
self.port = server_port
# Send HTTP POST with JSON payload and get response
def send_json_data(self, endpoint, payload):
conn = http.HTTPConnection(self.host, self.port, timeout=5000)
conn.request('POST', endpoint, payload, headers)
result = conn.getresponse()
response_data = json.loads(result.read().decode('utf-8'))
conn.close()
# Send JavaScript code to be executed remotely
def run_remote_script(self, js_code):
return self.send_json_data(endpoint, ('{"javascript":"' + js_code.decode('utf-8') + '"}').encode('utf-8'))
I’ve done something similar recently. Flask makes this way easier than raw HTTP connections. Set up a Flask server to handle receiving data from JavaScript and sending responses back. Use the fetch API on the JavaScript side - it’s much cleaner than XMLHttpRequest. Your timeout is way too long though. 5000ms for localhost? That’s excessive. 1-2 seconds is plenty for local communication. You need proper error handling for failed connections too. For security, you’re already binding to 127.0.0.1 instead of 0.0.0.0, which is good - that keeps it off your network. Just validate any data passing between applications to prevent injection issues. This is especially important if you’re executing JavaScript code remotely like your code suggests.
You need a proper HTTP server on the JS side to handle requests from Python. Node.js works great, but since you want vanilla JS, try Express or Python’s http.server module to serve your JS files. Then use fetch() to send data back to Python. Your code has a bug - send_json_data doesn’t return anything. Also, ditch http.client and use the requests library instead. Way cleaner. For JS responses, set CORS headers if you need them. Main security issue: always validate input. Never run arbitrary JavaScript without sanitizing first. If you’re doing frequent data exchange, WebSockets are better for real-time communication.
your approach is way too complicated. just spin up a simple python server with BaseHTTPServer or grab Bottle if u want something lightweight. then ur JS can make regular AJAX calls with XMLHttpRequest or fetch. the trick is having python listen for requests, not trying to keep connections alive. also, that timeout’s gonna bite u - localhost responds in milliseconds, not seconds.