Skip to content

Commit f43520a

Browse files
8 - Websocket Client in Python
1 parent 1a4b2b3 commit f43520a

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

client.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import asyncio
2+
import websockets
3+
import sys
4+
5+
websocket_endpoint = 'ws://localhost:8765' # url / uro
6+
7+
async def connect_to_ws(msg):
8+
async with websockets.connect(websocket_endpoint) as ws:
9+
await ws.send(msg)
10+
11+
if __name__=="__main__":
12+
msg = 'nothing here'
13+
try:
14+
msg = sys.argv[1]
15+
except:
16+
pass
17+
loop = asyncio.get_event_loop()
18+
loop.run_until_complete(connect_to_ws(msg))

index.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
import asyncio
22
import websockets
3+
import json
34

4-
async def webhook_handler(*args, **kwargs):
5-
print(args, kwargs)
6-
pass
5+
async def webhook_handler(websocket, path):
6+
print(websocket, path)
7+
async for message in websocket:
8+
data = {}
9+
try:
10+
data = json.loads(message)
11+
except:
12+
pass
13+
print(data.get('data'))
14+
# print(message)
15+
# pass
716

817

918
server = websockets.serve(webhook_handler, 'localhost', 8765)

0 commit comments

Comments
 (0)