16.01.2014 Views

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

308 CHAPTER 14 ■ NETWORK PROGRAMMING<br />

s.listen(5)<br />

p = select.poll()<br />

p.register(s)<br />

while True:<br />

events = p.poll()<br />

for fd, event in events:<br />

if fdmap[fd] is s:<br />

c, addr = s.accept()<br />

print 'Got connection from', addr<br />

p.register(c)<br />

fdmap[c.fileno()] = c<br />

elif event & select.POLLIN:<br />

data = fdmap[fd].recv(1024)<br />

if not data: # No data -- connection closed<br />

print fdmap[fd].getpeername(), 'disconnected'<br />

p.unregister(fd)<br />

del fdmap[fd]<br />

else:<br />

print data<br />

More Information<br />

You can find more information about select and poll in the <strong>Python</strong> Standard Library Reference<br />

(http://python.org/doc/lib/module-select.html). Also, reading the source code of the standard<br />

library modules asyncore and asynchat (found in the asyncore.py and asynchat.py files in<br />

your <strong>Python</strong> installation) can be enlightening.<br />

Twisted<br />

Twisted, from Twisted Matrix Labora<strong>to</strong>ries (http://twistedmatrix.com), is an event-driven<br />

networking framework for <strong>Python</strong>, originally developed for network games but now used by all<br />

kinds of network software. In Twisted, you implement event handlers, much like you would in<br />

a GUI <strong>to</strong>olkit (see Chapter 12). In fact, Twisted works quite nicely <strong>to</strong>gether with several common<br />

GUI <strong>to</strong>olkits (Tk, GTK, Qt, and wxWidgets). The Twisted documentation may not always be<br />

easy <strong>to</strong> navigate (although it’s continuously improving), but there is much information <strong>to</strong> be<br />

found on the Twisted Matrix Web site, twistedmatrix.com. In this section, I’ll cover some of the<br />

basic concepts and show you how <strong>to</strong> do some relatively simple network programming using<br />

Twisted. Once you grasp the basic concepts, you can check out the Twisted documentation <strong>to</strong><br />

do some more serious network programming. Twisted is a very rich framework and supports,<br />

among other things, Web servers and clients, SSH2, SMTP, POP3, IMAP4, AIM, ICQ, IRC, MSN,<br />

Jabber, NNTP, DNS, and more!<br />

Downloading and Installing Twisted<br />

Installing Twisted is quite easy. First, go <strong>to</strong> the Twisted Matrix Web page (http://<br />

twistedmatrix.com) and, from there, follow the link <strong>to</strong> the download page. If you’re using<br />

Windows, download the Windows installer for your version of <strong>Python</strong>; if you’re using some

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!