-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync_server_generators.py
62 lines (48 loc) · 1.66 KB
/
async_server_generators.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
import socket
from select import select
tasks = []
to_read = {}
to_write = {}
def server():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(('localhost', 8000))
server_socket.listen()
print('Server started. Listening...')
while True:
yield 'read', server_socket
client_socket, address = server_socket.accept()
print(f'Connection started: {address}')
tasks.append(client(client_socket, address))
def client(client_socket, address):
while True:
yield 'read', client_socket
request = client_socket.recv(1024)
if request:
message_in = request.decode('utf-8').rstrip()
print(f'Message from {address}: {message_in}')
yield 'write', client_socket
message_out = 'I read you.\n'.encode()
client_socket.send(message_out)
else:
break
client_socket.close()
print(f'Connection finished: {address}')
tasks.append(server())
while True:
while tasks:
try:
task = tasks.pop(0)
reason, sock = next(task)
if reason == 'read':
to_read[sock] = task
if reason == 'write':
to_write[sock] = task
except StopIteration:
# Connection closed and the socket was already removed from tasks list
pass
ready_to_read, ready_to_write, _ = select(to_read, to_write, [])
for sock in ready_to_read:
tasks.append(to_read.pop(sock))
for sock in ready_to_write:
tasks.append(to_write.pop(sock))