Last active
March 9, 2022 06:34
-
-
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
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
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