You are previewing Foundations of Python Network Programming, Third Edition.
O'Reilly logo
Foundations of Python Network Programming, Third Edition

Book Description

Foundations of Python Network Programming, Third Edition, covers all of the classic topics found in the second edition of this book, including network protocols, network data and errors, email, server architecture, and HTTP and web applications, plus updates for Python 3.

Some of the new topics in this edition include:

• Extensive coverage of the updated SSL support in Python 3

• How to write your own asynchronous I/O loop.

• An overview of the "asyncio" framework that comes with Python 3.4.

• How the Flask web framework connects URLs to your Python code.

• How cross-site scripting and cross-site request forgery can be used to attack your web site, and how to protect against them.

• How a full-stack web framework like Django can automate the round trip from your database to the screen and back.

If you're a Python programmer who needs a deep understanding of how to use Python for network-related tasks and applications, this is the book for you. From web application developers, to systems integrators, to system administrators—this book has everything that you need to know.

Table of Contents

  1. Cover
  2. Title
  3. Copyright
  4. Dedication
  5. Contents at a Glance
  6. Contents
  7. About the Authors
  8. About the Technical Reviewers
  9. Acknowledgments
  10. Introduction
  11. Chapter 1: Introduction to Client-Server Networking
    1. The Building Blocks: Stacks and Libraries
    2. Application Layers
    3. Speaking a Protocol
    4. A Raw Network Conversation
    5. Turtles All the Way Down
    6. Encoding and Decoding
    7. The Internet Protocol
    8. IP Addresses
    9. Routing
    10. Packet Fragmentation
    11. Learning More About IP
    12. Summary
  12. Chapter 2: UDP
    1. Port Numbers
    2. Sockets
      1. Promiscuous Clients and Unwelcome Replies
      2. Unreliability, Backoff, Blocking, and Timeouts
      3. Connecting UDP Sockets
      4. Request IDs: A Good Idea
    3. Binding to Interfaces
    4. UDP Fragmentation
    5. Socket Options
    6. Broadcast
      1. When to Use UDP
    7. Summary
  13. Chapter 3: TCP
    1. How TCP Works
    2. When to Use TCP
    3. What TCP Sockets Mean
    4. A Simple TCP Client and Server
      1. One Socket per Conversation
      2. Address Already in Use
    5. Binding to Interfaces
    6. Deadlock
    7. Closed Connections, Half-Open Connections
    8. Using TCP Streams Like Files
    9. Summary
  14. Chapter 4: Socket Names and DNS
    1. Hostnames and Sockets
      1. Five Socket Coordinates
      2. IPv6
    2. Modern Address Resolution
      1. Using getaddrinfo() to Bind Your Server to a Port
      2. Using getaddrinfo() to Connect to a Service
      3. Asking getaddrinfo() for a Canonical Hostname
      4. Other getaddrinfo() Flags
      5. Primitive Name Service Routines
      6. Using getsockaddr() in Your Own Code
    3. The DNS Protocol
      1. Why Not to Use Raw DNS
      2. Making a DNS Query from Python
      3. Resolving Mail Domains
    4. Summary
  15. Chapter 5: Network Data and Network Errors
    1. Bytes and Strings
      1. Character Strings
      2. Binary Numbers and Network Byte Order
    2. Framing and Quoting
    3. Pickles and Self-delimiting Formats
    4. XML and JSON
    5. Compression
    6. Network Exceptions
      1. Raising More Specific Exceptions
      2. Catching and Reporting Network Exceptions
    7. Summary
  16. Chapter 6: TLS/SSL
    1. What TLS Fails to Protect
    2. What Could Possibly Go Wrong?
    3. Generating Certificates
    4. Offloading TLS
    5. Python 3.4 Default Contexts
      1. Variations on Socket Wrapping
    6. Hand-Picked Ciphers and Perfect Forward Security
    7. Protocol Support for TLS
    8. Learning Details
    9. Summary
  17. Chapter 7: Server Architecture
    1. A Few Words About Deployment
    2. A Simple Protocol
    3. A Single-Threaded Server
    4. Threaded and Multiprocess Servers
      1. The Legacy SocketServer Framework
    5. Async Servers
      1. Callback-Style asyncio
      2. Coroutine-Style asyncio
      3. The Legacy Module asyncore
      4. The Best of Both Worlds
    6. Running Under inetd
    7. Summary
  18. Chapter 8: Caches and Message Queues
    1. Using Memcached
    2. Hashing and Sharding
    3. Message Queues
      1. Using Message Queues from Python
    4. Summary
  19. Chapter 9: HTTP Clients
    1. Python Client Libraries
    2. Ports, Encryption, and Framing
    3. Methods
    4. Paths and Hosts
    5. Status Codes
    6. Caching and Validation
    7. Content Encoding
    8. Content Negotiation
    9. Content Type
    10. HTTP Authentication
    11. Cookies
    12. Connections, Keep-Alive, and httplib
    13. Summary
  20. Chapter 10: HTTP Servers
    1. WSGI
    2. Asynchronous Server-Frameworks
    3. Forward and Reverse Proxies
    4. Four Architectures
      1. Running Python Under Apache
      2. The Rise of Pure-Python HTTP Servers
      3. The Benefits of Reverse Proxies
    5. Platforms as a Service
    6. GET and POST Patterns and the Question of REST
    7. WSGI Without a Framework
    8. Summary
  21. Chapter 11: The World Wide Web
    1. Hypermedia and URLs
      1. Parsing and Building URLs
      2. Relative URLs
    2. The Hypertext Markup Language
    3. Reading and Writing to a Database
    4. A Terrible Web Application (in Flask)
    5. The Dance of Forms and HTTP Methods
      1. When Forms Use Wrong Methods
      2. Safe and Unsafe Cookies
      3. Nonpersistent Cross-Site Scripting
      4. Persistent Cross-Site Scripting
      5. Cross-Site Request Forgery
      6. The Improved Application
    6. The Payments Application in Django
    7. Choosing a Web Framework
    8. WebSockets
    9. Web Scraping
      1. Fetching Pages
      2. Scraping Pages
      3. Recursive Scraping
    10. Summary
  22. Chapter 12: Building and Parsing E-Mail
    1. E-Mail Message Format
    2. Building an E-Mail Message
    3. Adding HTML and Multimedia
    4. Adding Content
    5. Parsing E-Mail Messages
    6. Walking MIME Parts
    7. Header Encodings
    8. Parsing Dates
    9. Summary
  23. Chapter 13: SMTP
    1. E-mail Clients vs. Webmail Services
      1. In the Beginning Was the Command Line
      2. The Rise of Clients
      3. The Move to Webmail
    2. How SMTP Is Used
      1. Sending E-Mail
      2. Headers and the Envelope Recipient
      3. Multiple Hops
    3. Introducing the SMTP Library
    4. Error Handling and Conversation Debugging
    5. Getting Information fromEHLO
    6. Using Secure Sockets Layer and Transport Layer Security
    7. Authenticated SMTP
    8. SMTP Tips
    9. Summary
  24. Chapter 14: POP
    1. POP Server Compatibility
    2. Connecting and Authenticating
    3. Obtaining Mailbox Information
    4. Downloading and Deleting Messages
    5. Summary
  25. Chapter 15: IMAP
    1. Understanding IMAP in Python
      1. IMAPClient
      2. Examining Folders
      3. Message Numbers vs. UIDs
      4. Message Ranges
      5. Summary Information
      6. Downloading an Entire Mailbox
      7. Downloading Messages Individually
      8. Flagging and Deleting Messages
      9. Deleting Messages
      10. Searching
      11. Manipulating Folders and Messages
      12. Asynchrony
    2. Summary
  26. Chapter 16: Telnet and SSH
    1. Command-Line Automation
      1. Command-Line Expansion and Quoting
      2. Unix Command Arguments Can Include (Almost) Any Character
      3. Quoting Characters for Protection
      4. The Terrible Windows Command Line
      5. Things Are Different in a Terminal
      6. Terminals Do Buffering
    2. Telnet
    3. SSH: The Secure Shell
      1. An Overview of SSH
      2. SSH Host Keys
      3. SSH Authentication
      4. Shell Sessions and Individual Commands
      5. SFTP: File Transfer Over SSH
      6. Other Features
    4. Summary
  27. Chapter 17: FTP
    1. What to Use Instead of FTP
      1. Communication Channels
      2. Using FTP in Python
      3. ASCII and Binary Files
      4. Advanced Binary Downloading
      5. Uploading Data
      6. Advanced Binary Uploading
      7. Handling Errors
      8. Scanning Directories
      9. Detecting Directories and Recursive Download
      10. Creating Directories, Deleting Things
      11. Doing FTP Securely
    2. Summary
  28. Chapter 18: RPC
    1. Features of RPC
      1. XML-RPC
      2. JSON-RPC
      3. Self-Documenting Data
      4. Talking About Objects: Pyro and RPyC
      5. An RPyC Example
      6. RPC, Web Frameworks, and Message Queues
      7. Recovering from Network Errors
    2. Summary
  29. Index