-
Notifications
You must be signed in to change notification settings - Fork 0
/
net_sel.py
executable file
·51 lines (44 loc) · 1.52 KB
/
net_sel.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
#!/usr/bin/env python
import select
import socket
import Queue
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setblocking(0)
server_address = ('', 2607)
server.bind(server_address)
server.listen(5)
inputs = [ server ]
outputs = [ ]
message_queues = {}
while inputs:
readable, writable, exceptional = select.select(inputs, outputs, [ ] )
for s in readable:
if s == server:
connection, client_address = s.accept()
print "New client from %s:%d connected!" % client_address
connection.setblocking(0)
inputs.append(connection)
message_queues[connection] = Queue.Queue()
else:
data = s.recv(1024)
if data:
print "Received %s from client %s:%d" % ((data.strip(),) + client_address)
if s not in outputs:
outputs.append(s)
message_queues[s].put(data)
else:
print "Client %s:%d disconnected!" % client_address
if s in outputs:
outputs.remove(s)
inputs.remove(s)
s.close()
del message_queues[s]
for s in writable:
try:
next_message = message_queues[s].get_nowait()
except Queue.Empty:
print "Nothing more to send to %s:%s" % s.getpeername()
outputs.remove(s)
else:
print "Sending %s to %s:%s" % ((next_message.strip(), ) + s.getpeername())
s.send(next_message)