Two approaches:
- sync with python-websocket-server
- asyncio with websockets
The sync approach is faster and less complex. ¯_(ツ)_/¯
Please comment if you have ideas about how to make the asyncio cleaner/faster.
First download the gist or copy the files to a folder.
Then install the two dependencies, I'm using pip3
and python3
here.
pip3 install websockets
pip3 install git+https://github.com/Pithikos/python-websocket-server
Run either asyncio or sync server:
# asyncio version
python3 ws-async.py
# or, sync version
python3 ws-sync.py
Once the websocket server is running, now let's run a simple HTTP server:
python3 -m http.server
Now open http://localhost:8000/ and check the console, you should get a stream of data from the websocket server.
MIT license.