Created
March 23, 2020 18:32
-
-
Save v-stickykeys/0473603bf2f3bf78d09fa195d9528245 to your computer and use it in GitHub Desktop.
simple-python-websocket
This file contains hidden or 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 | |
# WS server example | |
# https://websockets.readthedocs.io/en/stable/intro.html | |
""" | |
`pip install websockets` then run as a script | |
""" | |
import asyncio | |
import json | |
import time | |
import websockets | |
async def hello(websocket, path): | |
count = 0 | |
while True: | |
count += 1 | |
time.sleep(3) | |
await websocket.send(json.dumps({ | |
'data': 'here\'s some data!', | |
'count': count, | |
})) | |
print(f'> {count}') | |
start_server = websockets.serve(hello, 'localhost', 8765) | |
asyncio.get_event_loop().run_until_complete(start_server) | |
asyncio.get_event_loop().run_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment