clone3 commited on
Commit
ec48066
1 Parent(s): 69f903a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -21
app.py CHANGED
@@ -1,31 +1,28 @@
1
  import websockets
 
2
 
3
- def hello():
4
  uri = "ws://localhost:8765"
5
-
6
  # Connect to the WebSocket
7
- websocket = websockets.connect(uri)
 
 
 
 
8
 
9
- # Establish the connection
10
- websocket = websocket.__enter__()
11
 
12
- messages = [
13
- '{"fn_index":2,"session_hash":"kb46puzhzr"}',
14
- '{"data":["spider",1683852825,8,4,true],"event_data":null,"fn_index":2,"session_hash":"kb46puzhzr"}'
15
- ]
16
 
17
- for message in messages:
18
- print(f"Sending: {message}")
19
-
20
- # Send the message
21
- websocket.send(message)
22
-
23
- # Receive the response
24
- response = websocket.recv()
25
- print(f"Received: {response}")
26
 
27
- # Close the WebSocket connection
28
- websocket.__exit__()
29
 
30
  if __name__ == "__main__":
31
- hello()
 
1
  import websockets
2
+ import asyncio
3
 
4
+ async def hello():
5
  uri = "ws://localhost:8765"
6
+
7
  # Connect to the WebSocket
8
+ async with websockets.connect(uri) as websocket:
9
+ messages = [
10
+ '{"fn_index":2,"session_hash":"kb46puzhzr"}',
11
+ '{"data":["spider",1683852825,8,4,true],"event_data":null,"fn_index":2,"session_hash":"kb46puzhzr"}'
12
+ ]
13
 
14
+ for message in messages:
15
+ print(f"Sending: {message}")
16
 
17
+ # Send the message
18
+ await websocket.send(message)
 
 
19
 
20
+ # Receive the response
21
+ response = await websocket.recv()
22
+ print(f"Received: {response}")
 
 
 
 
 
 
23
 
24
+ async def main():
25
+ await hello()
26
 
27
  if __name__ == "__main__":
28
+ asyncio.run(main())