-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoll_echo_server.py
More file actions
52 lines (40 loc) · 1.42 KB
/
Copy pathpoll_echo_server.py
File metadata and controls
52 lines (40 loc) · 1.42 KB
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 socket
import select
import Queue
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setblocking(False)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_address = ('192.168.111.128', 10001)
print " Starting up on %s port %s "%server_address
server.bind(server_address)
server.listen(5)
message_queue = {}
# The timeout value is represented in milliseconds , instead of seconds
timeout = 1000
# Create a limit for the event
READ_ONLY = (select.POLLIN | select.POLLPRI | select.POLLHUP | select.POLLERR)
READ_WRITE = (READ_ONLY | select.POLLOUT)
# set up the poller
poller = select.poll()
poller.register(server, READ_ONLY)
# Map file descriptors to socket objects
fd_to_socket = {server.fileon():server,}
while True:
print "Waiting for the next event"
events = poller.poll(timeout)
print "*" * 20
print len(events)
print events
print "*" * 20
for fd, flag in events:
s = fd_to_socket[fd]
if flag & (select.POLLIN | select.POLLPRI):
if s is server:
connection, client_address = s.accept()
print " Connection " , client_address
connection.setblocking(False)
fd_to_socket[connection.fileno()] = connection
poller.register(connection, READ_ONLY)
message_queue[connection] = Queue.Queue()
else: