a_igin

socket example

Sep 24th, 2020
760
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import asyncio
  2. import websockets
  3. import json
  4.  
  5. ws_url = "ws://127.0.0.1:8000/ws/order_statuses/"
  6. token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6IjEyMzQiLCJpYXQiOjE1' \
  7.         'OTg4NTg4NTUsImV4cCI6NTkxODg1ODg1NSwianRpIjoiZjViNDM4MzQtYTUwOC00NDc5LWFjM2' \
  8.         'QtM2VkY2M2MzRmYmIwIiwidXNlcl9pZCI6MSwib3JpZ19pYXQiOjE1OTg4NTg4NTV9.WP_mv8c' \
  9.         'LHC61RarqGpEZMKUDgJd6eFI_wRe5zOqC0UE'
  10. auth_headers = {'Authorization': 'Bearer {}'.format(token)}
  11.  
  12.  
  13. async def command_receiver():
  14.     async with websockets.connect(ws_url, extra_headers=auth_headers) as websocket:
  15.         await websocket.send('ping')
  16.         message = await websocket.recv()
  17.         msg_as_json = json.loads(message)
  18.         print(msg_as_json['message'])
  19.         while True:
  20.             message = await websocket.recv()
  21.             print(message)
  22.  
  23.  
  24. asyncio.get_event_loop().run_until_complete(command_receiver())
  25.  
RAW Paste Data