Skip to content

Instantly share code, notes, and snippets.

@jamespo
Created March 23, 2018 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamespo/03634cc9307670ed637d1e9a55da8b07 to your computer and use it in GitHub Desktop.
Save jamespo/03634cc9307670ed637d1e9a55da8b07 to your computer and use it in GitHub Desktop.
test websockets server in python3
#!/usr/bin/env python
# test websockets - requires python 3.6+ & sanic
# start server & hit http://localhost:8000/static/WebSockets.html
# Put ws://localhost:8000/feed as Target
from sanic import Sanic
app = Sanic()
# create static dir & put
# https://raw.githubusercontent.com/ethicalhack3r/scripts/master/WebSockets.html
app.static('/static', './static')
@app.websocket('/feed')
async def feed(request, ws):
while True:
data = 'hello!'
print('Sending: ' + data)
await ws.send(data)
data = await ws.recv()
print('Received: ' + data)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment