Skip to main content
main-content

Über dieses Buch

This second edition of Foundations of Python Network Programming targets Python 2.5 through Python 2.7, the most popular production versions of the language. Python has made great strides since Apress released the first edition of this book back in the days of Python 2.3. The advances required new chapters to be written from the ground up, and others to be extensively revised.

You will learn fundamentals like IP, TCP, DNS and SSL by using working Python programs; you will also be able to familiarize yourself with infrastructure components like memcached and message queues. You can also delve into network server designs, and compare threaded approaches with asynchronous event-based solutions.

But the biggest change is this edition's expanded treatment of the web. The HTTP protocol is covered in extensive detail, with each feature accompanied by sample Python code. You can use your HTTP protocol expertise by studying an entire chapter on screen scraping and you can then test lxml and BeautifulSoup against a real-world web site. The chapter on web application programming now covers both the WSGI standard for component interoperability, as well as modern web frameworks like Django.

Finally, all of the old favorites from the first edition are back: E-mail protocols like SMTP, POP, and IMAP get full treatment, as does XML-RPC. You can still learn how to code Python network programs using the Telnet and FTP protocols, but you are likely to appreciate the power of more modern alternatives like the paramiko SSH2 library. If you are a Python programmer who needs to learn the network, this is the book that you want by your side.

Inhaltsverzeichnis

Frontmatter

CHAPTER 1 Introduction to Client/Server Networking

Abstract
This book is about network programming with the Python language: about accomplishing a specific set of tasks that all involve a particular technology—computer networks—using a general-purpose programming language that can do all sorts of things besides the things that you will see illustrated in this book.
Brandon Rhodes, John Goerzen

CHAPTER 2 UDP

Abstract
The previous chapter asserted that all network communications these days are built atop the transmission of short messages called packets that are usually no longer than a few thousand bytes. Packets each wing their way across the network independently, free to take different paths toward the same destination if redundant or load-balanced routers are part of the network. This means that packets can arrive out of order. If network conditions are poor, or a packet is simply unlucky, then it might easily not arrive at all.
Brandon Rhodes, John Goerzen

CHAPTER 3 TCP

Abstract
The Transmission Control Protocol (TCP) is the workhorse of the Internet. First defined in 1974, it lets applications send one another streams of data that, if they arrive at all—that is, unless a connection dies because of a network problem—are guaranteed to arrive intact, in order, and without duplication.
Brandon Rhodes, John Goerzen

CHAPTER 4 Socket Names and DNS

Abstract
Having spent the last two chapters learning the basics of UDP and TCP, the two major data transports available on IP networks, it is time for us to step back and talk about two larger issues that need to be tackled regardless of which transport you are using. In this chapter, we will discuss the topic of network addresses and will describe the distributed service that allows names to be resolved to raw IP addresses.
Brandon Rhodes, John Goerzen

CHAPTER 5 Network Data and Network Errors

Abstract
The first four chapters have given us a foundation: we have learned how hosts are named on an IP network, and we understand how to set up and tear down both TCP streams and UDP datagram connections between those hosts.
Brandon Rhodes, John Goerzen

CHAPTER 6 TLS and SSL

Abstract
The short story is this: before you send sensitive data across a network, you need proof of the identity of the machine that you think is on the other end of the socket, and while sending the data, you need it protected against the prying eyes of anyone controlling the gateways and network switches that see all of your packets. The solution to this problem is to use Transport Layer Security (TLS). Because earlier versions of TLS were called the Secure Sockets Layer (SSL), nearly all of the libraries that you will use to speak TLS actually still have SSL somewhere in the name.
Brandon Rhodes, John Goerzen

CHAPTER 7 Server Architecture

Abstract
This chapter explores how network programming intersects with the general tools and techniques that Python developers use to write long-running daemons that can perform significant amounts of work by keeping a computer and its processors busy.
Brandon Rhodes, John Goerzen

CHAPTER 8 Caches, Message Queues, and Map-Reduce

Abstract
This chapter, though brief, might be one of the most important in this book. It surveys the handful of technologies that have together become fundamental building blocks for expanding applications to Internet scale.
Brandon Rhodes, John Goerzen

CHAPTER 9 HTTP

Abstract
The protocols of yore tended to be dense, binary, and decipherable only by Boolean machine logic. But the workhorse protocol of the World Wide Web, named the Hypertext Transfer Protocol (HTTP), is instead based on friendly, mostly-human-readable text. There is probably no better way to start this chapter than to show you what an actual request and response looks like; that way, you will already know the layout of a whole request as we start digging into each of its features.
Brandon Rhodes, John Goerzen

CHAPTER 10 Screen Scraping

Abstract
Most web sites are designed first and foremost for human eyes. While well-designed sites offer formal APIs by which you can construct Google maps, upload Flickr photos, or browse YouTube videos, many sites offer nothing but HTML pages formatted for humans. If you need a program to be able to fetch its data, then you will need the ability to dive into densely formatted markup and retrieve the information you need—a process known affectionately as screen scraping.
Brandon Rhodes, John Goerzen

CHAPTER 11 Web Applications

Abstract
This chapter focuses on the actual act of programming—on what it means to sit down and write a Python web application. Every other issue that we consider will be in the service of this overarching goal: to create a new web service using Python as our language.
Brandon Rhodes, John Goerzen

CHAPTER 12 E-mail Composition and Decoding

Abstract
The early e-mail protocols were among the first network dialects developed for the Internet. The world was a simple one in those days: everyone with access to the Internet reached it through a command-line account on an Internet-connected machine. There, at the command line, they would type out e-mails to their friends, and then they could check their in-boxes when new mail arrived. The entire task of an email protocol was to transmit messages from one big Internet server to another, whenever someone sent mail to a friend whose shell account happened to be on a different machine.
Brandon Rhodes, John Goerzen

CHAPTER 14 SMTP

Abstract
As we outlined at the beginning of the previous chapter, the actual movement of e-mail between systems is accomplished through SMTP: the “Simple Mail Transport Protocol.” It was first defined in 1982 in RFC 821; the most recent RFC defining it is 5321. It typically serves in two roles:
  • When a user types an e-mail message on a laptop or desktop machine, the e-mail client uses SMTP to submit the e-mail to a real server that can send it along to its destination.
  • E-mail servers themselves use SMTP to deliver messages, sending them across the Internet to the server in charge of the recipient e-mail address’s domain (the part of the e-mail address after the @ sign).
Brandon Rhodes, John Goerzen

CHAPTER 14 POP

Abstract
POP, the Post Office Protocol, is a simple protocol that is used to download e-mail from a mail server, and is typically used through an e-mail client like Thunderbird or Outlook. You can read the first few sections of Chapter 13 if you want the big picture of where e-mail clients, and protocols like POP, fit into the history of Internet mail.
Brandon Rhodes, John Goerzen

CHAPTER 15 IMAP

Abstract
At first glance, the Internet Message Access Protocol (IMAP) resembles the POP protocol described in Chapter 14. And if you have read the first sections of Chapter 13, which give the whole picture of how email travels across the Internet, you will already know that the two protocols fill a quite similar role: POP and IMAP are two ways that a laptop or desktop computer can connect to a larger Internet server to view and manipulate a user’s e-mail.
Brandon Rhodes, John Goerzen

CHAPTER 16 Telnet and SSH

Abstract
If you have never read it, then you should brew some of your favorite coffee, sit down, and treat yourself to reading Neal Stephenson’s essay “In the Beginning Was the Command Line.” You can download a copy from his web site, in the form—appropriately enough—of a raw text file: http://www.cryptonomicon.com/beginning.html.
Brandon Rhodes, John Goerzen

CHAPTER 17 FTP

Abstract
The File Transfer Protocol (FTP) was once among the most widely used protocols on the Internet, invoked whenever a user wanted to transfer files between Internet-connected computers.
Brandon Rhodes, John Goerzen

CHAPTER 18 RPC

Abstract
Remote Procedure Call (RPC) systems let you call a remote function using the same syntax that you would use when calling a routine in a local API or library. This tends to be useful in two situations:
  • Your program has a lot of work to do, and you want to spread it across several machines by making calls across the network.
  • You need data or information that is only available on another hard drive or network, and an RPC interface lets you easily send queries to another system to get back an answer.
Brandon Rhodes, John Goerzen

Backmatter

Weitere Informationen

Premium Partner

    Bildnachweise