1import select
2import socket
3import sys
4import Queue
5
6# Create a TCP/IP socket
7server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
8server.setblocking(0)
9
10# Bind the socket to the port
11server_addr = ('localhost', 10000)
12print('starting server on {server_addr[0]}:{server_addr[1]}')
13server.bind(server_address)
14server.listen(5)
15
16# Sockets from which we expect to read
17inputs = [ server ]
18
19# Sockets to which we expect to write
20outputs = [ ]
21
22# Outgoing message queues (socket:Queue)
23message_queues = {}
24
25while inputs:
26
27 # Wait for at least one of the sockets to be ready for processing
28 print('waiting for the next event')
29 readable, writable, exceptional = select.select(inputs, outputs, inputs)
30
31 # Handle inputs
32 for s in readable:
33 if s is server:
34 # A "readable" server socket is ready to accept a connection
35 connection, client_address = s.accept()
36 print(f'new connection from {client_address}')
37 connection.setblocking(False)
38 inputs.append(connection)
39
40 # Give the connection a queue for data we want to send
41 message_queues[connection] = Queue.Queue()
42 else:
43 data = s.recv(1024)
44 if data:
45 # A readable client socket has data
46 print(f'received "{data}" from {s.getpeername()}')
47 message_queues[s].put(data)
48 # Add output channel for response
49 if s not in outputs:
50 outputs.append(s)
51 else:
52 # Interpret empty result as closed connection
53 print(f'closing {client_address} after reading no data')
54 # Stop listening for input on the connection
55 if s in outputs:
56 outputs.remove(s)
57 inputs.remove(s)
58 s.close()
59
60 # Remove message queue
61 del message_queues[s]
62
63 # Handle outputs
64 for s in writable:
65 try:
66 next_msg = message_queues[s].get_nowait()
67 except Queue.Empty:
68 # No messages waiting so stop checking for writability.
69 print(f'output queue for {s.getpeername()} is empty')
70 outputs.remove(s)
71 else:
72 print(f'sending "{next_msg}" to {s.getpeername()}')
73 s.send(next_msg)
74
75 # Handle "exceptional conditions"
76 for s in exceptional:
77 print(f'handling exceptional condition for {s.getpeername()}')
78 # Stop listening for input on the connection
79 inputs.remove(s)
80 if s in outputs:
81 outputs.remove(s)
82 s.close()
83 # Remove message queue
84 del message_queues[s]