Skip to content

Instantly share code, notes, and snippets.

@ankitml
Last active November 11, 2018 13:12
Show Gist options
  • Save ankitml/b9d4a07af07cf8f98fd7f7947595f7c4 to your computer and use it in GitHub Desktop.
Save ankitml/b9d4a07af07cf8f98fd7f7947595f7c4 to your computer and use it in GitHub Desktop.
import asyncio
import websockets
from websockets.exceptions import ConnectionClosed
from time import sleep
async def connect():
while True:
try:
async with websockets.connect('ws://localhost:9999') as websocket:
async for message in websocket:
print(message)
except ConnectionClosed:
# blocking sleep
print('Connection closed, trying to reconnecting in few seconds')
sleep(5)
except OSError:
# blocking sleep
print('Cannot find websocket server, retrying in 15 seconds')
sleep(15)
asyncio.get_event_loop().run_until_complete(connect())
@ankitml
Copy link
Author

ankitml commented Nov 10, 2018

A simple websocket client (python3.7). Perhaps 3.5/3.6 are also ok, but I have not tested it.

@ankitml
Copy link
Author

ankitml commented Nov 11, 2018

Dependencies:

pip install websockets

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment