The select Module
This select module, shown in Example 7-6, allows you to check for incoming data on one or more
sockets, pipes, or other compatible stream objects.
You can pass one or more sockets to the select
function to wait for them to become readable, writable, or signal an
error:
A socket becomes ready for reading when someone connects after a call to
listen(which means thatacceptwon’t block) when data arrives from the remote end, or when the socket is closed or reset (in this case,recvwill return an empty string).A socket becomes ready for writing when the connection is established after a non-blocking call to
connector when data can be written to the socket.A socket signals an error condition when the connection fails after a non-blocking call to
connect.
Example 7-6. Using the select Module to Wait for Data Arriving Over Sockets
File: select-example-1.py
import select
import socket
import time
PORT = 8037
TIME1970 = 2208988800L
service = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
service.bind(("", PORT))
service.listen(1)
print "listening on port", PORT
while 1:
is_readable = [service]
is_writable = []
is_error = []
r, w, e = select.select(is_readable, is_writable, is_error, 1.0)
if r:
channel, info = service.accept()
print "connection from", info
t = int(time.time()) + TIME1970
t = chr(t>>24&255) + chr(t>>16&255) + chr(t>>8&255) + chr(t&255)
channel.send(t) # send timestamp
channel.close() # disconnect
else:
print "still waiting"
listening on port 8037 ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access