Skip to content

Instantly share code, notes, and snippets.

@v-stickykeys
Created March 23, 2020 18:32
Show Gist options
  • Save v-stickykeys/0473603bf2f3bf78d09fa195d9528245 to your computer and use it in GitHub Desktop.
Save v-stickykeys/0473603bf2f3bf78d09fa195d9528245 to your computer and use it in GitHub Desktop.
simple-python-websocket
#!/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