This repository has been archived by the owner on Oct 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathclient.py
76 lines (58 loc) · 2.14 KB
/
client.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
WebSockets client
This program receives simulated data for multiple rooms, with multiple sensors per room.
The default behavior is to only access the computer itself by parameter "localhost"
so that no firewall edits are needed.
The port number is arbitrary, as long as the server and client are on the same port all is well.
Naturally, the server must be started before this client attempts to connect.
"""
import websockets
import zlib
from pathlib import Path
import argparse
import asyncio
async def main(port: int, addr: str, max_packets: int, log_file: Path = None):
"""
Parameters
----------
port: int
the network port to use (arbitrary, must match server)
addr: str
the address of the server (localhost if on same computer)
max_packets: int
to avoid using all the hard drive if the client is left running,
we set a maximum number of packets before shutting the client down
log_file: pathlib.Path
where to store the data received (student must add code for this)
"""
uri = f"ws://{addr}:{port}"
async with websockets.connect(uri) as websocket:
qb = await websocket.recv()
if isinstance(qb, bytes):
print(zlib.decompress(qb).decode("utf8"))
else:
print(qb)
if log_file:
log_file = Path(log_file).expanduser()
raise NotImplementedError("The code to open the file should be here.")
for _ in range(max_packets):
data = await websocket.recv()
print(data)
def cli():
p = argparse.ArgumentParser(description="WebSocket client")
p.add_argument("-l", "--log", help="file to log JSON data")
p.add_argument("-host", help="Host address", default="localhost")
p.add_argument("-port", help="network port", type=int, default=8765)
p.add_argument(
"-max_packets",
help="shut down program after total packages received",
type=int,
default=100000,
)
P = p.parse_args()
try:
asyncio.run(main(P.port, P.host, P.max_packets, P.log))
except KeyboardInterrupt:
print(P.log)
if __name__ == "__main__":
cli()