Skip to content

Instantly share code, notes, and snippets.

@hongsolo9
Last active March 9, 2022 06:34
Show Gist options
  • Save hongsolo9/48c7c4d699061ca6eafd7a031cf8b95d to your computer and use it in GitHub Desktop.
Save hongsolo9/48c7c4d699061ca6eafd7a031cf8b95d to your computer and use it in GitHub Desktop.
Sample Websockets code in python; Receive via the data variable; Requirements: pip3 install asyncio websockets
import asyncio
import websockets
import json
# Requirements
# pip3 install asyncio websockets
# TODO: Replace wss endpoint
stream = 'wss://stream...'
async def main():
async with websockets.connect(stream) as ws:
while True:
try:
data = await ws.recv()
# Wss data comes out here
print(f"{data}")
except TypeError as e:
print(f"TypeError: {e}")
except KeyboardInterrupt as e:
print(f"KeyboardInterrupt: {e}"
if __name__ == "__main__":
loop.asyncio.new_event_loop()
loop.run_until_completion(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment