-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
52 lines (35 loc) · 1.33 KB
/
server.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
import socket, selectors, queue
from importlib import reload
import monitor, client
def server_callback_r(key, event):
conn, addr = server.accept()
conn.setblocking(False)
sel = key.data[1]
sel.register(conn, selectors.EVENT_READ, (client_callback, *key.data[1:3], addr))
print(f"New connection recieved from {addr}")
def monitor_callback(key, event):
reload(monitor) # remove this in the final thing, but very fun for testing
return monitor.monitor_callback_rw(key, event)
def client_callback(key, event):
reload(client)
return client.client_callback_rw(key, event)
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("", 8080))
server.listen()
print("Please connect monitor")
monitor_soc, addr = server.accept()
print("Monitor connected")
server.setblocking(False)
monitor_soc.setblocking(False)
write_queues = {}
sel = selectors.DefaultSelector()
sel.register(server, selectors.EVENT_READ, (server_callback_r, sel, write_queues))
sel.register(monitor_soc, selectors.EVENT_READ, (monitor_callback, sel, write_queues, (server, server_callback_r)))
running = True
while running:
for key, event in sel.select(None): # None means block until something to do
callback = key.data[0]
if callback(key, event) == "QUIT":
running = False
sel.close()
server.close()