Foundations of Python network programming [3rd ed] 9781430258551, 1430258551

To guide readers through the new scripting language, Python, this book discusses every aspect of client and server progr

1,551 174 4MB

English Pages [369] Year 2014

Report DMCA / Copyright

DOWNLOAD FILE

Foundations of Python network programming [3rd ed]
 9781430258551, 1430258551

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
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
Summary......Page 44
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
Summary......Page 61
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
Using getsockaddr() in Your Own Code......Page 71
The DNS Protocol......Page 73
Making a DNS Query from Python......Page 75
Resolving Mail Domains......Page 77
Summary......Page 79
Bytes and Strings......Page 80
Character Strings......Page 81
Binary Numbers and Network Byte Order......Page 84
Framing and Quoting......Page 86
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
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
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
Running Under inetd......Page 137
Summary......Page 139
Using Memcached......Page 140
Hashing and Sharding......Page 143
Message Queues......Page 145
Using Message Queues from Python......Page 147
Summary......Page 151
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 Negotiation......Page 163
HTTP Authentication......Page 165
Cookies......Page 167
Connections, Keep-Alive, and httplib......Page 168
Summary......Page 169
WSGI......Page 170
Forward and Reverse Proxies......Page 172
Four Architectures......Page 173
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
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
Safe and Unsafe Cookies......Page 200
Nonpersistent Cross-Site Scripting......Page 202
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
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
Summary......Page 240
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
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
Summary......Page 258
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 Ranges......Page 273
Summary Information......Page 274
Downloading an Entire Mailbox......Page 275
Downloading Messages Individually......Page 277
Deleting Messages......Page 283
Searching......Page 284
Manipulating Folders and Messages......Page 285
Asynchrony......Page 286
Summary......Page 287
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
Things Are Different in a Terminal......Page 294
Terminals Do Buffering......Page 297
Telnet......Page 298
SSH: The Secure Shell......Page 302
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
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
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
Recovering from Network Errors......Page 344
Summary......Page 345
Index......Page 346

Polecaj historie