-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
64 lines (45 loc) · 1.46 KB
/
test.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
# test.py
from socketsio import Client, Server, Socket, BHP, TCP, UDP
from looperation import Handler, Operator
def action(server: Server, client: Socket) -> None:
"""
Sets or updates clients data in the clients' container .
:param server: The server controlling the communication.
:param client: The client socket object.
"""
with Handler(
exception_handler=print,
cleanup_callback=lambda h: client.close()
):
while not (client.closed or server.closed):
received, address = client.receive()
if not received:
continue
print("server:", (received, address))
sent = (
f"server received from "
f"{address}: ".encode() + received
)
client.send(sent)
HOST = "127.0.0.1"
PROTOCOL = 'TCP'
PORT = 5000
def main() -> None:
"""Tests the program."""
if PROTOCOL == 'UDP':
protocol = UDP()
elif PROTOCOL == 'TCP':
protocol = BHP(TCP())
else:
raise ValueError(f"Invalid protocol type: {PROTOCOL}")
server = Server(protocol)
server.bind((HOST, PORT))
service = Operator(operation=lambda: server.handle(action=action))
service.run()
client = Client(protocol)
client.connect((HOST, PORT))
for _ in range(2):
client.send((", ".join(["hello world"] * 3)).encode())
print("client:", client.receive())
if __name__ == '__main__':
main()