Created
March 23, 2018 15:27
-
-
Save jamespo/03634cc9307670ed637d1e9a55da8b07 to your computer and use it in GitHub Desktop.
test websockets server in python3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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