I'm trying to create a reverse proxy in Python, and got it working for the normal HTTP traffic, but can't figure out how to proxy web sockets. I was hoping flask_sockets could do this, but I can't figure out how to do it. Is it possible, and how would you do that? I tried to do like this, e.g. (using, in addition, websocket):
@sockets.route('/2/')
@sockets.route('/2/')
def proxy_socket(ws_in):
path = ws_in.environ['QUERY_STRING']
message = ws_in.receive()
ws_path = f'{SITE_NAME2_WS}{path}'
ws = create_connection(ws_path)
ws.send(message)
result = ws.recv()
while not ws.closed:
ws_in.send(result)
ws.close()
But this didn't work, I'm able to read messages sent on ws_in, but it fails to connect and send them to the backend. Anyway, if you have any guidance on how to proxy websockets in Python, I'd appreciate it. Thanks.
I'm trying to create a reverse proxy in Python, and got it working for the normal HTTP traffic, but can't figure out how to proxy web sockets. I was hoping flask_sockets could do this, but I can't figure out how to do it. Is it possible, and how would you do that? I tried to do like this, e.g. (using, in addition, websocket):
@sockets.route('/2/') @sockets.route('/2/')
def proxy_socket(ws_in):
path = ws_in.environ['QUERY_STRING']
message = ws_in.receive()
ws_path = f'{SITE_NAME2_WS}{path}'
ws = create_connection(ws_path)
ws.send(message)
result = ws.recv()
while not ws.closed:
ws_in.send(result)
ws.close()
But this didn't work, I'm able to read messages sent on ws_in, but it fails to connect and send them to the backend. Anyway, if you have any guidance on how to proxy websockets in Python, I'd appreciate it. Thanks.