Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 14 ■ NETWORK PROGRAMMING 311 class SimpleLogger(LineReceiver): def connectionMade(self): print 'Got connection from', self.transport.client def connectionLost(self, reason): print self.transport.client, 'disconnected' def lineReceived(self, line): print line factory = Factory() factory.protocol = SimpleLogger reactor.listenTCP(1234, factory) reactor.run() There is a lot more to the Twisted framework than what I’ve shown you here. If you’re interested in learning more, you should check out the online documentation, available at the Twisted Web site (http://twistedmatrix.com). A Quick Summary This chapter has given you a taste of several approaches to network programming in Python. Which approach you choose will depend on your specific needs and preferences. Once you’ve chosen, you will, most likely, need to learn more about the specific method than I can explain in this chapter. Here are some of the topics this chapter touched upon: Sockets and the socket module. Sockets are information channels that let programs (processes) communicate, possibly across a network. The socket module gives you lowlevel access to both client and server sockets. Server sockets listen at a given address for client connections, while clients simply connect directly. urllib and urllib2. These modules let you read and download data from various servers, given a URL to the data source. The urllib module is a simpler implementation, while urllib2 is very extensible and quite powerful. Both work through straightforward functions such as urlopen. The SocketServer framework. This is a network of synchronous server base classes, found in the standard library, which lets you write servers quite easily. There is even support for simple Web (HTTP) servers with CGI. If you want to handle several connections simultaneously, you need to use a forking or threading mix-in class. select and poll. These two functions let you consider a set of connections and find out which ones are ready for reading and writing. This means that you can serve several connections piecemeal, in a round-robin fashion. This gives the illusion of handling several connections at the same time, and, although superficially a bit more complicated to code, is a much more scalable and efficient solution than threading or forking.
312 CHAPTER 14 ■ NETWORK PROGRAMMING Twisted. This framework, from Twisted Matrix Laboratories, is very rich and complex, with support for most major network protocols. Even though it is large, and some of the idioms used may seem a bit foreign, basic usage is very simple and intuitive. The Twisted framework is also asynchronous, so it’s very efficient and scalable. If you have Twisted available, it may very well be the best choice for many custom network applications. New Functions in This Chapter Function urllib.urlopen(url[, data[, proxies]]) urllib.urlretrieve(url[, fname[, hook[, data]]]) urllib.quote(string[, safe]) Description Opens a file-like object from a URL Downloads a file from a URL Quotes special URL characters urllib.quote_plus(string[, safe]) The same, but quotes space as + urllib.unquote(string) urllib.unquote_plus(string) urllib.urlencode(query[, doseq]) select.select(iseq, oseq, eseq[, timeout]) select.poll() reactor.listenTCP(port, factory) reactor.run() The reverse of quote The reverse of quote_plus Encodes mapping for use in CGI queries Finds sockets ready for reading/writing Creates a poll object, for polling sockets Twisted function; listens for connections Twisted function; main server loop What Now? You thought we were finished with network stuff now, huh? Not a chance. The next chapter deals with a quite specialized and much-publicized entity in the world of networking: the Web.
- Page 291 and 292: 260 CHAPTER 11 ■ FILES AND STUFF
- Page 293 and 294: 262 CHAPTER 11 ■ FILES AND STUFF
- Page 295 and 296: 264 CHAPTER 11 ■ FILES AND STUFF
- Page 297 and 298: 266 CHAPTER 11 ■ FILES AND STUFF
- Page 299 and 300: 268 CHAPTER 11 ■ FILES AND STUFF
- Page 301 and 302: 270 CHAPTER 12 ■ GRAPHICAL USER I
- Page 303 and 304: 272 CHAPTER 12 ■ GRAPHICAL USER I
- Page 305 and 306: 274 CHAPTER 12 ■ GRAPHICAL USER I
- Page 307 and 308: 276 CHAPTER 12 ■ GRAPHICAL USER I
- Page 309 and 310: 278 CHAPTER 12 ■ GRAPHICAL USER I
- Page 311 and 312: 280 CHAPTER 12 ■ GRAPHICAL USER I
- Page 313 and 314: 282 CHAPTER 12 ■ GRAPHICAL USER I
- Page 316 and 317: CHAPTER 13 ■ ■ ■ Database Sup
- Page 318 and 319: CHAPTER 13 ■ DATABASE SUPPORT 287
- Page 320 and 321: CHAPTER 13 ■ DATABASE SUPPORT 289
- Page 322 and 323: CHAPTER 13 ■ DATABASE SUPPORT 291
- Page 324 and 325: CHAPTER 13 ■ DATABASE SUPPORT 293
- Page 326: CHAPTER 13 ■ DATABASE SUPPORT 295
- Page 329 and 330: 298 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 331 and 332: 300 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 333 and 334: 302 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 335 and 336: 304 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 337 and 338: 306 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 339 and 340: 308 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 341: 310 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 345 and 346: 314 CHAPTER 15 ■ PYTHON AND THE W
- Page 347 and 348: 316 CHAPTER 15 ■ PYTHON AND THE W
- Page 349 and 350: 318 CHAPTER 15 ■ PYTHON AND THE W
- Page 351 and 352: 320 CHAPTER 15 ■ PYTHON AND THE W
- Page 353 and 354: 322 CHAPTER 15 ■ PYTHON AND THE W
- Page 355 and 356: 324 CHAPTER 15 ■ PYTHON AND THE W
- Page 357 and 358: 326 CHAPTER 15 ■ PYTHON AND THE W
- Page 359 and 360: 328 CHAPTER 15 ■ PYTHON AND THE W
- Page 361 and 362: 330 CHAPTER 15 ■ PYTHON AND THE W
- Page 363 and 364: 332 CHAPTER 15 ■ PYTHON AND THE W
- Page 365 and 366: 334 CHAPTER 15 ■ PYTHON AND THE W
- Page 367 and 368: 336 CHAPTER 15 ■ PYTHON AND THE W
- Page 369 and 370: 338 CHAPTER 15 ■ PYTHON AND THE W
- Page 372 and 373: CHAPTER 16 ■ ■ ■ Testing, 1-2
- Page 374 and 375: CHAPTER 16 ■ TESTING, 1-2-3 343 W
- Page 376 and 377: CHAPTER 16 ■ TESTING, 1-2-3 345 d
- Page 378 and 379: CHAPTER 16 ■ TESTING, 1-2-3 347 u
- Page 380 and 381: CHAPTER 16 ■ TESTING, 1-2-3 349 F
- Page 382 and 383: CHAPTER 16 ■ TESTING, 1-2-3 351 P
- Page 384 and 385: CHAPTER 16 ■ TESTING, 1-2-3 353 "
- Page 386: CHAPTER 16 ■ TESTING, 1-2-3 355 N
- Page 389 and 390: 358 CHAPTER 17 ■ EXTENDING PYTHON
- Page 391 and 392: 360 CHAPTER 17 ■ EXTENDING PYTHON
312 CHAPTER 14 ■ NETWORK PROGRAMMING<br />
Twisted. This framework, from Twisted Matrix Labora<strong>to</strong>ries, is very rich and complex,<br />
with support for most major network pro<strong>to</strong>cols. Even though it is large, and some of the<br />
idioms used may seem a bit foreign, basic usage is very simple and intuitive. The Twisted<br />
framework is also asynchronous, so it’s very efficient and scalable. If you have Twisted<br />
available, it may very well be the best choice for many cus<strong>to</strong>m network applications.<br />
New Functions in This Chapter<br />
Function<br />
urllib.urlopen(url[, data[, proxies]])<br />
urllib.urlretrieve(url[, fname[, hook[, data]]])<br />
urllib.quote(string[, safe])<br />
Description<br />
Opens a file-like object from a URL<br />
Downloads a file from a URL<br />
Quotes special URL characters<br />
urllib.quote_plus(string[, safe]) The same, but quotes space as +<br />
urllib.unquote(string)<br />
urllib.unquote_plus(string)<br />
urllib.urlencode(query[, doseq])<br />
select.select(iseq, oseq, eseq[, timeout])<br />
select.poll()<br />
reac<strong>to</strong>r.listenTCP(port, fac<strong>to</strong>ry)<br />
reac<strong>to</strong>r.run()<br />
The reverse of quote<br />
The reverse of quote_plus<br />
Encodes mapping for use in CGI<br />
queries<br />
Finds sockets ready for<br />
reading/writing<br />
Creates a poll object, for polling<br />
sockets<br />
Twisted function; listens for<br />
connections<br />
Twisted function; main server loop<br />
What Now?<br />
You thought we were finished with network stuff now, huh? Not a chance. The next chapter<br />
deals with a quite specialized and much-publicized entity in the world of networking: the Web.