add a python websocket proxy which works on Linux, while ws proxy_server does not

This commit is contained in:
Benjamin Sergeant
2019-11-18 13:46:11 -08:00
parent 901c21e499
commit cc492bf1a3
10 changed files with 131 additions and 39 deletions

View File

@ -0,0 +1 @@
0.0.1

View File

@ -0,0 +1,8 @@
FROM python:3.8.0-alpine3.10
RUN pip install websockets
COPY ws_proxy.py /usr/bin
RUN chmod +x /usr/bin/ws_proxy.py
EXPOSE 8765
CMD ["python", "/usr/bin/ws_proxy.py"]

View File

@ -0,0 +1,20 @@
.PHONY: docker
NAME := bsergean/ws_proxy
TAG := $(shell cat DOCKER_VERSION)
IMG := ${NAME}:${TAG}
LATEST := ${NAME}:latest
BUILD := ${NAME}:build
docker_test:
docker build -t ${BUILD} .
docker:
git clean -dfx
docker build -t ${IMG} .
docker tag ${IMG} ${BUILD}
docker_push:
docker tag ${IMG} ${LATEST}
docker push ${LATEST}
docker push ${IMG}

View File

@ -0,0 +1,43 @@
#!/usr/bin/env python3
# WS server example
import argparse
import asyncio
import websockets
async def hello(websocket, path):
url = REMOTE_URL + path
async with websockets.connect(url) as ws:
taskA = asyncio.create_task(clientToServer(ws, websocket))
taskB = asyncio.create_task(serverToClient(ws, websocket))
await taskA
await taskB
async def clientToServer(ws, websocket):
async for message in ws:
await websocket.send(message)
async def serverToClient(ws, websocket):
async for message in websocket:
await ws.send(message)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='websocket proxy.')
parser.add_argument('--host', help='Host to bind to.', default='localhost')
parser.add_argument('--port', help='Port to bind to.', default=8765)
parser.add_argument('--remote_url', help='Remote websocket url', default='ws://localhost:8767')
args = parser.parse_args()
REMOTE_URL = args.remote_url
start_server = websockets.serve(hello, args.host, args.port)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()