Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 14 ■ ■ ■ Network Programming In this chapter, I give you a sample of the various ways in which Python can help you write programs that use a network such as the Internet as an important component. Python is a very powerful tool for network programming, for many reasons. First, there are many preexisting libraries for common network protocols and for various layers of abstractions on top of them, so you can concentrate on the logic of your program, rather than on shuffling bits across wires. Second, it’s easy to write code for handling various protocol formats that may not have existing code, because Python’s really good at tackling various patterns in byte streams (you’ve already seen this in dealing with text files in various ways). Because Python has such an abundance of network tools available for you to use, I can only give you a brief peek at its networking capabilities here. You can find some other examples elsewhere in this book: I discuss the urllib module in Chapter 10, you get a discussion of Weboriented network programming in Chapter 15, and several of the projects use various networking modules to get the job done. If you want to know even more about network programming in Python, I can heartily recommend John Goerzen’s Foundations of Python Network Programming (Apress, 2004), which deals with the subject very thoroughly. In the following sections, I give you an overview of some of the networking modules available in the Python standard library. Then comes a discussion of the SocketServer class and its friends, followed by a brief look at the various ways in which you can handle several connections at once. Finally, I give you a look at the Twisted framework, a rich and mature framework for writing networked applications in Python. ■Note If you’ve got a strict firewall in place, it will probably warn you once you start running your own network programs; it will probably stop them from connecting to the network. You should either configure it to let your Python do its work, or, if the firewall has an interactive interface (such as the Windows XP firewall), simply allow the connections when asked. Note, though, that any software connected to a network is a potential security risk, even if (or especially if) you wrote the software yourself. A Handful of Networking Modules There are plenty of networking modules available in the standard library, and many more available elsewhere. In addition to those that clearly deal mainly with networking, many 297
298 CHAPTER 14 ■ NETWORK PROGRAMMING modules (such as those that deal with various forms of data encoding for network transport) may be seen as network-related. I’ve been fairly restrictive in my selection of modules here. socket A basic component in network programming is the socket. A socket is basically an “information channel” with a program on both ends. The programs may be on different computers (connected through a network) and may send information to each other through the socket. Most network programming in Python hides the basic workings of the socket module, and doesn’t interact with the sockets directly. Sockets come in two varieties: server sockets and client sockets. After you create a server socket, you tell it to wait for connections. It will then listen at a certain network address (a combination of an IP address and a port number) until a client socket connects. The two can then communicate. Dealing with client sockets is usually quite a bit easier than dealing with the server side, because the server has to be ready to deal with clients whenever they connect, and it must deal with multiple connections, while the client simply connects, does its thing, and disconnects. Later in this chapter, I deal with server programming through the SocketServer class family and the Twisted framework. A socket is an instance of the socket class from the socket module. It is instantiated with up to three parameters: an address family (defaulting to socket.AF_INET), whether it’s a stream (socket.SOCK_STREAM, the default) or a datagram (socket.SOCK_DGRAM) socket, and a protocol (defaulting to 0, which should be okay). For a plain-vanilla socket, you don’t really have to supply any arguments. A server socket uses its bind method followed by a call to listen to listen to a given address. A client socket can then connect to the server by using its connect method with the same address as used in bind. (On the server side, you can, for example, get the name of the current machine using the function socket.gethostname.) In this case, an address is just a tuple of the form (host, port), where host is a host name (such as www.example.com) and port is a port number (an integer). The listen method takes a single argument, which is the length of its backlog (the number of connections allowed to queue up, waiting for acceptance, before connections start being disallowed). Once a server socket is listening, it can start accepting clients. This is done using the accept method. This method will block (wait) until a client connects, and then it will return a tuple of the form (client, address), where client is a client socket and address is an address, as explained earlier. The server can then deal with the client as it sees fit, and then start waiting for new connections, with another call to accept. This is usually done in an infinite loop. ■Note This form of server programming is called blocking or synchronous network programming. Later, you’ll see examples of nonblocking or asynchronous network programming, as well as using threads to be able to deal with several clients at once.
- Page 276 and 277: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 278 and 279: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 280 and 281: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 282 and 283: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 284: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 287 and 288: 256 CHAPTER 11 ■ FILES AND STUFF
- Page 289 and 290: 258 CHAPTER 11 ■ FILES AND STUFF
- 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 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 and 342: 310 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 343 and 344: 312 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
298 CHAPTER 14 ■ NETWORK PROGRAMMING<br />
modules (such as those that deal with various forms of data encoding for network transport)<br />
may be seen as network-related. I’ve been fairly restrictive in my selection of modules here.<br />
socket<br />
A basic component in network programming is the socket. A socket is basically an “information<br />
channel” with a program on both ends. The programs may be on different computers (connected<br />
through a network) and may send information <strong>to</strong> each other through the socket. Most network<br />
programming in <strong>Python</strong> hides the basic workings of the socket module, and doesn’t interact<br />
with the sockets directly.<br />
Sockets come in two varieties: server sockets and client sockets. After you create a server<br />
socket, you tell it <strong>to</strong> wait for connections. It will then listen at a certain network address (a<br />
combination of an IP address and a port number) until a client socket connects. The two can<br />
then communicate.<br />
Dealing with client sockets is usually quite a bit easier than dealing with the server side,<br />
because the server has <strong>to</strong> be ready <strong>to</strong> deal with clients whenever they connect, and it must deal<br />
with multiple connections, while the client simply connects, does its thing, and disconnects.<br />
Later in this chapter, I deal with server programming through the SocketServer class family<br />
and the Twisted framework.<br />
A socket is an instance of the socket class from the socket module. It is instantiated with<br />
up <strong>to</strong> three parameters: an address family (defaulting <strong>to</strong> socket.AF_INET), whether it’s a stream<br />
(socket.SOCK_STREAM, the default) or a datagram (socket.SOCK_DGRAM) socket, and a pro<strong>to</strong>col<br />
(defaulting <strong>to</strong> 0, which should be okay). For a plain-vanilla socket, you don’t really have <strong>to</strong><br />
supply any arguments.<br />
A server socket uses its bind method followed by a call <strong>to</strong> listen <strong>to</strong> listen <strong>to</strong> a given address.<br />
A client socket can then connect <strong>to</strong> the server by using its connect method with the same address as<br />
used in bind. (On the server side, you can, for example, get the name of the current machine<br />
using the function socket.gethostname.) In this case, an address is just a tuple of the form<br />
(host, port), where host is a host name (such as www.example.com) and port is a port number<br />
(an integer). The listen method takes a single argument, which is the length of its backlog (the<br />
number of connections allowed <strong>to</strong> queue up, waiting for acceptance, before connections start<br />
being disallowed).<br />
Once a server socket is listening, it can start accepting clients. This is done using the accept<br />
method. This method will block (wait) until a client connects, and then it will return a tuple<br />
of the form (client, address), where client is a client socket and address is an address, as<br />
explained earlier. The server can then deal with the client as it sees fit, and then start waiting for<br />
new connections, with another call <strong>to</strong> accept. This is usually done in an infinite loop.<br />
■Note This form of server programming is called blocking or synchronous network programming. Later,<br />
you’ll see examples of nonblocking or asynchronous network programming, as well as using threads <strong>to</strong> be<br />
able <strong>to</strong> deal with several clients at once.