Foundations of Python Network Programming

369 Pages · 3.71 mb ·

Programming

Table of contents

- Contents at a Glance (Page 3)
- Contents (Page 358)
- About the Author (Page 367)
- About the Technical Reviewers (Page 368)
- Acknowledgments (Page 369)
- Introduction (Page 5)
- Chapter 1: Introduction to Client-Server Networking (Page 9)
- The Building Blocks: Stacks and Libraries (Page 9)
- Application Layers (Page 12)
- Speaking a Protocol (Page 13)
- A Raw Network Conversation (Page 14)
- Turtles All the Way Down (Page 17)
- Encoding and Decoding (Page 18)
- The Internet Protocol (Page 19)
- IP Addresses (Page 20)
- Routing (Page 21)
- Packet Fragmentation (Page 22)
- Learning More About IP (Page 23)
- Summary (Page 24)
- Chapter 2: UDP (Page 25)
- Port Numbers (Page 26)
- Sockets (Page 27)
- Promiscuous Clients and Unwelcome Replies (Page 31)
- Unreliability, Backoff, Blocking, and Timeouts (Page 32)
- Connecting UDP Sockets (Page 36)
- Request IDs: A Good Idea (Page 37)
- Binding to Interfaces (Page 38)
- UDP Fragmentation (Page 40)
- Socket Options (Page 41)
- Broadcast (Page 42)
- When to Use UDP (Page 44)
- Summary (Page 44)
- Chapter 3: TCP (Page 46)
- How TCP Works (Page 46)
- When to Use TCP (Page 47)
- What TCP Sockets Mean (Page 48)
- A Simple TCP Client and Server (Page 49)
- One Socket per Conversation (Page 52)
- Address Already in Use (Page 53)
- Binding to Interfaces (Page 54)
- Deadlock (Page 55)
- Closed Connections, Half-Open Connections (Page 60)
- Using TCP Streams Like Files (Page 61)
- Summary (Page 61)
- Chapter 4: Socket Names and DNS (Page 63)
- Hostnames and Sockets (Page 63)
- Five Socket Coordinates (Page 64)
- IPv6 (Page 65)
- Modern Address Resolution (Page 66)
- Using getaddrinfo() to Bind Your Server to a Port (Page 67)
- Using getaddrinfo() to Connect to a Service (Page 68)
- Asking getaddrinfo() for a Canonical Hostname (Page 69)
- Other getaddrinfo() Flags (Page 70)
- Primitive Name Service Routines (Page 71)
- Using getsockaddr() in Your Own Code (Page 71)
- The DNS Protocol (Page 73)
- Why Not to Use Raw DNS (Page 75)
- Making a DNS Query from Python (Page 75)
- Resolving Mail Domains (Page 77)
- Summary (Page 79)
- Chapter 5: Network Data and Network Errors (Page 80)
- Bytes and Strings (Page 80)
- Character Strings (Page 81)
- Binary Numbers and Network Byte Order (Page 84)
- Framing and Quoting (Page 86)
- Pickles and Self-delimiting Formats (Page 91)
- XML and JSON (Page 91)
- Compression (Page 92)
- Network Exceptions (Page 93)
- Raising More Specific Exceptions (Page 95)
- Catching and Reporting Network Exceptions (Page 96)
- Summary (Page 97)
- Chapter 6: TLS/SSL (Page 98)
- What TLS Fails to Protect (Page 98)
- What Could Possibly Go Wrong? (Page 99)
- Generating Certificates (Page 100)
- Offloading TLS (Page 103)
- Python 3.4 Default Contexts (Page 104)
- Variations on Socket Wrapping (Page 108)
- Hand-Picked Ciphers and Perfect Forward Security (Page 109)
- Protocol Support for TLS (Page 111)
- Learning Details (Page 112)
- Summary (Page 118)
- Chapter 7: Server Architecture (Page 119)
- A Few Words About Deployment (Page 119)
- A Simple Protocol (Page 121)
- A Single-Threaded Server (Page 124)
- Threaded and Multiprocess Servers (Page 127)
- The Legacy SocketServer Framework (Page 128)
- Async Servers (Page 129)
- Callback-Style asyncio (Page 132)
- Coroutine-Style asyncio (Page 134)
- The Legacy Module asyncore (Page 135)
- The Best of Both Worlds (Page 137)
- Running Under inetd (Page 137)
- Summary (Page 139)
- Chapter 8: Caches and Message Queues (Page 140)
- Using Memcached (Page 140)
- Hashing and Sharding (Page 143)
- Message Queues (Page 145)
- Using Message Queues from Python (Page 147)
- Summary (Page 151)
- Chapter 9: HTTP Clients (Page 153)
- Python Client Libraries (Page 153)
- Ports, Encryption, and Framing (Page 155)
- Methods (Page 156)
- Paths and Hosts (Page 157)
- Status Codes (Page 158)
- Caching and Validation (Page 160)
- Content Encoding (Page 163)
- Content Negotiation (Page 163)
- Content Type (Page 165)
- HTTP Authentication (Page 165)
- Cookies (Page 167)
- Connections, Keep-Alive, and httplib (Page 168)
- Summary (Page 169)
- Chapter 10: HTTP Servers (Page 170)
- WSGI (Page 170)
- Asynchronous Server- Frameworks (Page 172)
- Forward and Reverse Proxies (Page 172)
- Four Architectures (Page 173)
- Running Python Under Apache (Page 175)
- The Rise of Pure-Python HTTP Servers (Page 175)
- The Benefits of Reverse Proxies (Page 175)
- Platforms as a Service (Page 176)
- GET and POST Patterns and the Question of REST (Page 177)
- WSGI Without a Framework (Page 179)
- Summary (Page 183)
- Chapter 11: The World Wide Web (Page 184)
- Hypermedia and URLs (Page 184)
- Parsing and Building URLs (Page 185)
- Relative URLs (Page 187)
- The Hypertext Markup Language (Page 189)
- Reading and Writing to a Database (Page 192)
- A Terrible Web Application (in Flask) (Page 193)
- The Dance of Forms and HTTP Methods (Page 198)
- When Forms Use Wrong Methods (Page 200)
- Safe and Unsafe Cookies (Page 200)
- Nonpersistent Cross-Site Scripting (Page 202)
- Persistent Cross-Site Scripting (Page 204)
- Cross-Site Request Forgery (Page 204)
- The Improved Application (Page 205)
- The Payments Application in Django (Page 207)
- Choosing a Web Framework (Page 211)
- WebSockets (Page 212)
- Web Scraping (Page 213)
- Fetching Pages (Page 214)
- Scraping Pages (Page 217)
- Recursive Scraping (Page 219)
- Summary (Page 223)
- Chapter 12: Building and Parsing E-Mail (Page 224)
- E-Mail Message Format (Page 224)
- Building an E-Mail Message (Page 226)
- Adding HTML and Multimedia (Page 228)
- Adding Content (Page 233)
- Parsing E-Mail Messages (Page 234)
- Walking MIME Parts (Page 236)
- Header Encodings (Page 238)
- Parsing Dates (Page 240)
- Summary (Page 240)
- Chapter 13: SMTP (Page 241)
- E-mail Clients vs. Webmail Services (Page 241)
- In the Beginning Was the Command Line (Page 241)
- The Rise of Clients (Page 242)
- The Move to Webmail (Page 243)
- How SMTP Is Used (Page 244)
- Sending E-Mail (Page 246)
- Headers and the Envelope Recipient (Page 246)
- Multiple Hops (Page 247)
- Introducing the SMTP Library (Page 248)
- Error Handling and Conversation Debugging (Page 249)
- Getting Information from EHLO (Page 252)
- Using Secure Sockets Layer and Transport Layer Security (Page 254)
- Authenticated SMTP (Page 256)
- SMTP Tips (Page 258)
- Summary (Page 258)
- Chapter 14: POP (Page 259)
- POP Server Compatibility (Page 259)
- Connecting and Authenticating (Page 260)
- Obtaining Mailbox Information (Page 263)
- Downloading and Deleting Messages (Page 264)
- Summary (Page 266)
- Chapter 15: IMAP (Page 267)
- Understanding IMAP in Python (Page 268)
- IMAPClient (Page 270)
- Examining Folders (Page 272)
- Message Numbers vs. UIDs (Page 273)
- Message Ranges (Page 273)
- Summary Information (Page 274)
- Downloading an Entire Mailbox (Page 275)
- Downloading Messages Individually (Page 277)
- Flagging and Deleting Messages (Page 283)
- Deleting Messages (Page 283)
- Searching (Page 284)
- Manipulating Folders and Messages (Page 285)
- Asynchrony (Page 286)
- Summary (Page 287)
- Chapter 16: Telnet and SSH (Page 288)
- Command-Line Automation (Page 288)
- Command-Line Expansion and Quoting (Page 289)
- Unix Command Arguments Can Include (Almost) Any Character (Page 290)
- Quoting Characters for Protection (Page 293)
- The Terrible Windows Command Line (Page 294)
- Things Are Different in a Terminal (Page 294)
- Terminals Do Buffering (Page 297)
- Telnet (Page 298)
- SSH: The Secure Shell (Page 302)
- An Overview of SSH (Page 303)
- SSH Host Keys (Page 303)
- SSH Authentication (Page 305)
- Shell Sessions and Individual Commands (Page 306)
- SFTP: File Transfer Over SSH (Page 311)
- Other Features (Page 313)
- Summary (Page 314)
- Chapter 17: FTP (Page 315)
- What to Use Instead of FTP (Page 315)
- Communication Channels (Page 316)
- Using FTP in Python (Page 317)
- ASCII and Binary Files (Page 318)
- Advanced Binary Downloading (Page 319)
- Uploading Data (Page 321)
- Advanced Binary Uploading (Page 322)
- Handling Errors (Page 323)
- Scanning Directories (Page 324)
- Detecting Directories and Recursive Download (Page 326)
- Creating Directories, Deleting Things (Page 327)
- Doing FTP Securely (Page 328)
- Summary (Page 328)
- Chapter 18: RPC (Page 329)
- Features of RPC (Page 330)
- XML-RPC (Page 331)
- JSON-RPC (Page 337)
- Self-Documenting Data (Page 340)
- Talking About Objects: Pyro and RPyC (Page 341)
- An RPyC Example (Page 342)
- RPC, Web Frameworks, and Message Queues (Page 344)
- Recovering from Network Errors (Page 344)
- Summary (Page 345)
- Index (Page 346)