-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_client_cli.py
More file actions
55 lines (45 loc) · 1.47 KB
/
Copy pathtest_client_cli.py
File metadata and controls
55 lines (45 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from __future__ import annotations
import asyncio
import json
import socket
import aioconsole
import wsdatautil
ws_stream_reader = wsdatautil.ProgressiveStreamReader("auto")
addr = ("localhost", 8881)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(addr)
async def connect():
asyncio.get_running_loop().set_debug(True)
r, w = await asyncio.open_connection(sock=sock)
handshake = wsdatautil.HandshakeRequest()
w.write(handshake.to_streamdata())
await w.drain()
handshake_data = await r.readuntil(b'\r\n\r\n')
print("hs:", handshake_data)
async def writer():
while True:
data = await aioconsole.ainput('{}?> ')
try:
obj = eval(data)
f = wsdatautil.Frame(payload=json.dumps(obj).encode())
d = f.to_streamdata()
w.write(d)
await w.drain()
except Exception as e:
print(e)
async def reader():
while True:
print("...")
var = 2
while isinstance(var, int):
try:
d = await r.readexactly(var)
print("d:", d)
except Exception as e:
print("e:", e)
return
var = ws_stream_reader.progressive_read(d)
frame: wsdatautil.Frame = var
print("f:", frame)
await asyncio.gather(writer(), reader())
asyncio.run(connect())