You are previewing Foundations of Python Network Programming: The comprehensive guide to building network applications with Python, Second Edition.
O'Reilly logo
Foundations of Python Network Programming: The comprehensive guide to building network applications with Python, Second Edition

Book Description

This book gives you everything you need to know about network programming using Python 3, the latest version of the Python language. It will benefit both scripters and serious application developers who want a feature-rich, yet simple language. Fully updated, this second edition includes all the new developments in network programming such as WSGI, FastCGI, and asynchronous communication.

Foundations of Python 3 Network Programming, Second Edition explains multitasking network servers using several models, including forking, threading, and non-blocking sockets. Extensive examples throughout the book demonstrate important concepts and practices, and provide a cadre of fully-functioning stand alone programs. Readers may even use the examples included as building blocks to create their own software.

Table of Contents

  1. Copyright
  2. About the Authors
  3. About the Technical Reviewer
  4. Acknowledgements
  5. Introduction
    1. Assumptions
    2. Networking
    3. Organization
    4. Program Listings
    5. Your Comments
  6. 1. Introduction to Client/Server Networking
    1. 1.1. The Building Blocks: Stacks and Libraries
    2. 1.2. Application Layers
    3. 1.3. Speaking a Protocol
    4. 1.4. A Raw Network Conversation
    5. 1.5. Turtles All the Way Down
    6. 1.6. The Internet Protocol
    7. 1.7. IP Addresses
    8. 1.8. Routing
    9. 1.9. Packet Fragmentation
    10. 1.10. Learning More About IP
  7. 2. UDP
    1. 2.1. Should You Read This Chapter?
    2. 2.2. Addresses and Port Numbers
    3. 2.3. Port Number Ranges
    4. 2.4. Sockets
    5. 2.5. Unreliability, Backoff, Blocking, Timeouts
    6. 2.6. Connecting UDP Sockets
    7. 2.7. Request IDs: A Good Idea
    8. 2.8. Binding to Interfaces
    9. 2.9. UDP Fragmentation
    10. 2.10. Socket Options
    11. 2.11. Broadcast
    12. 2.12. When to Use UDP
    13. 2.13. Summary
  8. 3. TCP
    1. 3.1. How TCP Works
    2. 3.2. When to Use TCP
    3. 3.3. What TCP Sockets Mean
    4. 3.4. A Simple TCP Client and Server
    5. 3.5. One Socket per Conversation
    6. 3.6. Address Already in Use
    7. 3.7. Binding to Interfaces
    8. 3.8. Deadlock
    9. 3.9. Closed Connections, Half-Open Connections
    10. 3.10. Using TCP Streams like Files
    11. 3.11. Summary
  9. 4. Socket Names and DNS
    1. 4.1. Hostnames and Domain Names
    2. 4.2. Socket Names
    3. 4.3. Five Socket Coordinates
    4. 4.4. IPv6
    5. 4.5. Modern Address Resolution
    6. 4.6. Asking getaddrinfo() Where to Bind
    7. 4.7. Asking getaddrinfo() About Services
    8. 4.8. Asking getaddrinfo() for Pretty Hostnames
    9. 4.9. Other getaddrinfo() Flags
    10. 4.10. Primitive Name Service Routines
    11. 4.11. Using getsockaddr() in Your Own Code
    12. 4.12. Better Living Through Paranoia
    13. 4.13. A Sketch of How DNS Works
    14. 4.14. Why Not to Use DNS
    15. 4.15. Why to Use DNS
    16. 4.16. Resolving Mail Domains
    17. 4.17. Zeroconf and Dynamic DNS
    18. 4.18. Summary
  10. 5. Network Data and Network Errors
    1. 5.1. Text and Encodings
    2. 5.2. Network Byte Order
    3. 5.3. Framing and Quoting
    4. 5.4. Pickles and Self-Delimiting Formats
    5. 5.5. XML, JSON, Etc.
    6. 5.6. Compression
    7. 5.7. Network Exceptions
    8. 5.8. Handling Exceptions
    9. 5.9. Summary
  11. 6. TLS and SSL
    1. 6.1. Computer Security
    2. 6.2. IP Access Rules
    3. 6.3. Cleartext on the Network
    4. 6.4. TLS Encrypts Your Conversations
    5. 6.5. TLS Verifies Identities
    6. 6.6. Supporting TLS in Python
    7. 6.7. The Standard SSL Module
    8. 6.8. Loose Ends
    9. 6.9. Summary
  12. 7. Server Architecture
    1. 7.1. Daemons and Logging
    2. 7.2. Our Example: Sir Launcelot
    3. 7.3. An Elementary Client
    4. 7.4. The Waiting Game
    5. 7.5. Running a Benchmark
    6. 7.6. Event-Driven Servers
    7. 7.7. Poll vs. Select
    8. 7.8. The Semantics of Non-blocking
    9. 7.9. Event-Driven Servers Are Blocking and Synchronous
    10. 7.10. Twisted Python
    11. 7.11. Load Balancing and Proxies
    12. 7.12. Threading and Multi-processing
    13. 7.13. Threading and Multi-processing Frameworks
    14. 7.14. Process and Thread Coordination
    15. 7.15. Running Inside inetd
    16. 7.16. Summary
  13. 8. Caches, Message Queues, and Map-Reduce
    1. 8.1. Using Memcached
    2. 8.2. Memcached and Sharding
    3. 8.3. Message Queues
    4. 8.4. Using Message Queues from Python
    5. 8.5. How Message Queues Change Programming
    6. 8.6. Map-Reduce
    7. 8.7. Summary
  14. 9. HTTP
    1. 9.1. URL Anatomy
    2. 9.2. Relative URLs
    3. 9.3. Instrumenting urllib2
    4. 9.4. The GET Method
    5. 9.5. The Host Header
    6. 9.6. Codes, Errors, and Redirection
    7. 9.7. Payloads and Persistent Connections
    8. 9.8. POST And Forms
    9. 9.9. Successful Form POSTs Should Always Redirect
    10. 9.10. POST And APIs
    11. 9.11. REST And More HTTP Methods
    12. 9.12. Identifying User Agents and Web Servers
    13. 9.13. Content Type Negotiation
    14. 9.14. Compression
    15. 9.15. HTTP Caching
    16. 9.16. The HEAD Method
    17. 9.17. HTTPS Encryption
    18. 9.18. HTTP Authentication
    19. 9.19. Cookies
    20. 9.20. HTTP Session Hijacking
    21. 9.21. Cross-Site Scripting Attacks
    22. 9.22. WebOb
    23. 9.23. Summary
  15. 10. Screen Scraping
    1. 10.1. Fetching Web Pages
    2. 10.2. Downloading Pages Through Form Submission
    3. 10.3. The Structure of Web Pages
    4. 10.4. Three Axes
    5. 10.5. Diving into an HTML Document
    6. 10.6. Selectors
    7. 10.7. Summary
  16. 11. Web Applications
    1. 11.1. Web Servers and Python
    2. 11.2. Two Tiers
    3. 11.3. Choosing a Web Server
    4. 11.4. WSGI
    5. 11.5. WSGI Middleware
    6. 11.6. Python Web Frameworks
    7. 11.7. URL Dispatch Techniques
    8. 11.8. Templates
    9. 11.9. Final Considerations
    10. 11.10. Pure-Python Web Servers
    11. 11.11. CGI
    12. 11.12. mod_python
    13. 11.13. Summary
  17. 12. E-mail Composition and Decoding
    1. 12.1. E-mail Messages
    2. 12.2. Composing Traditional Messages
    3. 12.3. Parsing Traditional Messages
    4. 12.4. Parsing Dates
    5. 12.5. Understanding MIME
    6. 12.6. How MIME Works
    7. 12.7. Composing MIME Attachments
    8. 12.8. MIME Alternative Parts
    9. 12.9. Composing Non-English Headers
    10. 12.10. Composing Nested Multiparts
    11. 12.11. Parsing MIME Messages
    12. 12.12. Decoding Headers
    13. 12.13. Summary
  18. 13. SMTP
    1. 13.1. E-mail Clients, Webmail Services
      1. 13.1.1. In the Beginning Was the Command Line
      2. 13.1.2. The Rise of Clients
      3. 13.1.3. The Move to Webmail
    2. 13.2. How SMTP Is Used
    3. 13.3. Sending E-Mail
    4. 13.4. Headers and the Envelope Recipient
    5. 13.5. Multiple Hops
    6. 13.6. Introducing the SMTP Library
    7. 13.7. Error Handling and Conversation Debugging
    8. 13.8. Getting Information from EHLO
    9. 13.9. Using Secure Sockets Layer and Transport Layer Security
    10. 13.10. Authenticated SMTP
    11. 13.11. SMTP Tips
    12. 13.12. Summary
  19. 14. POP
    1. 14.1. Compatibility Between POP Servers
    2. 14.2. Connecting and Authenticating
    3. 14.3. Obtaining Mailbox Information
    4. 14.4. Downloading and Deleting Messages
    5. 14.5. Summary
  20. 15. IMAP
    1. 15.1. Understanding IMAP in Python
    2. 15.2. IMAPClient
    3. 15.3. Examining Folders
    4. 15.4. Message Numbers vs. UIDs
    5. 15.5. Message Ranges
    6. 15.6. Summary Information
    7. 15.7. Downloading an Entire Mailbox
    8. 15.8. Downloading Messages Individually
    9. 15.9. Flagging and Deleting Messages
    10. 15.10. Deleting Messages
    11. 15.11. Searching
    12. 15.12. Manipulating Folders and Messages
    13. 15.13. Asynchrony
    14. 15.14. Summary
  21. 16. Telnet and SSH
    1. 16.1. Command-Line Automation
    2. 16.2. Command-Line Expansion and Quoting
    3. 16.3. Unix Has No Special Characters
    4. 16.4. Quoting Characters for Protection
    5. 16.5. The Terrible Windows Command Line
    6. 16.6. Things Are Different in a Terminal
    7. 16.7. Terminals Do Buffering
    8. 16.8. Telnet
    9. 16.9. SSH: The Secure Shell
    10. 16.10. An Overview of SSH
    11. 16.11. SSH Host Keys
    12. 16.12. SSH Authentication
    13. 16.13. Shell Sessions and Individual Commands
    14. 16.14. SFTP: File Transfer Over SSH
    15. 16.15. Other Features
    16. 16.16. Summary
  22. 17. FTP
    1. 17.1. What to Use Instead of FTP
    2. 17.2. Communication Channels
    3. 17.3. Using FTP in Python
    4. 17.4. ASCII and Binary Files
    5. 17.5. Advanced Binary Downloading
    6. 17.6. Uploading Data
    7. 17.7. Advanced Binary Uploading
    8. 17.8. Handling Errors
    9. 17.9. Detecting Directories and Recursive Download
    10. 17.10. Creating Directories, Deleting Things
    11. 17.11. Doing FTP Securely
    12. 17.12. Summary
  23. 18. RPC
    1. 18.1. Features of RPC
    2. 18.2. XML-RPC
    3. 18.3. JSON-RPC
    4. 18.4. Self-documenting Data
    5. 18.5. Talking About Objects: Pyro and RPyC
    6. 18.6. An RPyC Example
    7. 18.7. RPC, Web Frameworks, Message Queues
    8. 18.8. Recovering From Network Errors
    9. 18.9. Binary Options: Thrift and Protocol Buffers
    10. 18.10. Summary