Praise for Foundations of Python Network Programming: "This may be the easiest book review I've ever written. Ify ou program in Python and you want to write Internet applications, go buy Foundations of Python Network Programming by John Goerzen." -Ron Dippold, Slashdot contributor (www.slashdot.org) "Foundations of Python Networking Programming is highly recom mended ify ou already have a familiarity with networking concepts and basic Python programming. This is a second level book that should be used to extend your knowledge and programming skills after first learning the basics of Python with something like Dive Into Python." -Harold McFarland, Midwest Book Review (www.midwestbookreview.com) "This book makes an excellent addition to your Python bookshelf" -Jack Herrington, Code Generation (www.codegeneration.net) "I am happy to have this book on my shelf" - Tommi Virtanen, Tv's cobweb (http:/ /tv.debian.net) Foundations of Python Network Programming JOHN GOERZEN APress Media, LLC Foundations of Python Network Programming Copyright © 2004 by John Goerzen Originally published by Apress in 2004 All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN 978-1-59059-371-4 ISBN 978-1-4302-0752-8 (eBook) DOI 10.1007/978-1-4302-0752-8 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names ooly in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Iason Gilmore Technical Reviewer: Magnus tie Hetland Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Tony Davis, Iohn Franklin, Iason Gilmore, Chris Mills, Steve Rycroft, Dominic Shakeshaft, Iim Sumser, Karen Watterson, Gavin Wray, Iohn Zukowski Project Manager: Beth Christmas Copy Edit Manager: Nicole LeClerc Copy Editor: Mark Nigara Production Manager: Karl Brooks Production Editor: Ellie Fountain Compositor: Susan Glinert Stevens Proofreader: Elizabeth Berry Indexer: Kevin Broccoli Cover Designer: Kurt Krames Manufacturing Manager: Tom Debolski Distributed to the book trade in the United States by Springer-Verlag New York, Inc., 175 Fifth Avenue, New York, NY 10010 and outside the United States by Springer-Verlag GmbH & Co. KG, Tiergartenstr. 17,69112 Heidelberg, Germany. In the United States: phone 1-800-SPRINGER, e-mail orders@springer-ny _c orn, or visit http:/ /'tMt/. springer-ny. corn. Outside the United States: fax +496221 345229, e-mail [email protected], or visit http://'tMt/ • springer. de. The information in this book is distributed on an "as is" basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. To my wife Terah: Thank you for tolerating the tall grass, late evenings, and short weekends while I worked on this book. Thank you for supporting me every step of the way with encouragement, love, and chocolate cake. Contents at a Glance About the Author ................................................................................................. xv About the Technical Reviewer .................................................................... xvii Acknowledgments ................................................................................................. xix Introduction ....................................................................................................... xxi Part One Low-Level Networking .................................................... 1 Chapter 1 Introduction to Client/Server Networking .............. 3 Chapter 2 Network Clients ................................................................... 19 Chapter 3 Network Servers ................................................................... 35 Chapter 4 Domain Name System ............................................................ 65 Chapter 5 Advanced Network Operations ......................................... 87 Part Two Web Services ..................................................................... 111 Chapter 6 Web Client Access ............................................................. 113 Chapter 7 Parsing HTML and XHTML .................................................. 127 Chapter 8 XML and XML-RPC ................................................................. 145 Part Three E-mail Services ............................................................. 167 Chapter 9 E-Mail Composition and Decoding ........................................................................ 169 Chapter 10 Simple Message Transport Protocol .......................... 197 Chapter 11 POP ........................................................................................... 211 Chapter 12 IMAP ......................................................................................... 223 Part Four General-Purpose Client Protocols ................. 273 Chapter 13 FTP ........................................................................................... 275 Chapter 14 Database Clients ............................................................... 295 Chapter 15 SSL ........................................................................................... 321 v Contents at a Glance Part Five Server-Side Frameworks ........................................... 339 Chapter 16 SocketServer ........................................................................ 341 Chapter 17 SimpleXMLRPCServer ........................................................... 355 Chapter 18 CGI ........................................................................................... 369 Chapter 19 mod_python ............................................................................ 393 Part Six Multitasking ..................................................................... 417 Chapter 20 Forking ................................................................................... 419 Chapter 21 Threading .............................................................................. 443 Chapter 22 Asynchronous Communication ......................................... 469 Index ...................................................................................................................... 491 vi Contents About the Author .......................................................................................... xv About the Technical Reviewer ........................................................ xvii Acknowledgments ........................................................................................... xix Introduction .................................................................................................. xxi Part One Low-Level Networking ............................................. ! Chapter 1 Introduction to Client/Server Networking ........................................ 3 Understanding TCP Basics ................................................................................ 3 Using the Client/Server Model ..................................................................... 6 Understanding User Datagram Protocol ...................................................... 7 Understanding Physical Transports and Ethernet ................................ 9 Networking in Python ......................................................................................... 9 Summary ................................................................................................................... 17 Chapter 2 Network Clients ............................................................. 19 Understanding Sockets ..................................................................................... 19 Creating Sockets ................................................................................................ 20 Communicating with Sockets .......................................................................... 23 Handling Errors .................................................................................................. 23 Using User Datagram Protocol ...................................................................... 31 Summary ................................................................................................................... 34 vii Contents Chapter 3 Network Servers ............................................................. 35 Preparing for Connections ............................................................................ 35 Accepting Connections ..................................................................................... 40 Handling Errors .................................................................................................. 41 Using User Datagram Protocol ...................................................................... 43 Using inetd or xinetd ..................................................................................... 45 Logging with syslog ......................................................................................... 55 Avoiding Deadlock .............................................................................................. 60 Summary .................................................................................................................... 63 Chapter 4 Domain Name System ..................................................... 65 Making DNS Queries ............................................................................................ 65 Using Operating System Lookup Services ................................................ 66 Using PyDNS for Advanced Lookups ............................................................. 76 Summary .................................................................................................................... 85 Chapter 5 Advanced Network Operations .............................. 87 Half-Open Sockets .............................................................................................. 87 Timeouts ................................................................................................................. 89 Transmitting Strings ....................................................................................... 90 Understanding Network Byte Order ............................................................. 93 Using Broadcast Data ....................................................................................... 95 Working with IPv6 .............................................................................................. 97 Binding to Speci fie Addresses .................................................................. 102 Using Event Notification with poll() or select() ........................ 104 Summary .................................................................................................................. 109 Part Two Web Services ........................................................... 111 Chapter 6 Web Client Access ...................................................... 113 Fetching Web Pages .......................................................................................... 114 Authenticating .................................................................................................. 115 Submitting Form Data ..................................................................................... 118 Handling Errors ................................................................................................ 121 Using Non-HTTP Protocols ............................................................................. 125 Summary .................................................................................................................. 125 viii Contents Chapter 7 Parsing HTML and XHTML ........................................ 127 Understanding Basic HTML Parsing ........................................................... 128 Handling Real-World HTML ............................................................................ 130 A Working Example ............................................................................................ 137 Summary ................................................................................................................. 143 Chapter 8 XML and XML-RPC ........................................................... 145 Understanding XML Documents ...................................................................... 147 Using DOM ............................................................................................................. 148 Using XML-RPC .................................................................................................... 159 Summary ................................................................................................................. 166 Part Three E-mail Services ..................................................... l67 Chapter 9 E-Mail Composition and Decoding ................. 169 Understanding Traditional Messages ...................................................... 169 Composing Traditional Messages ............................................................... 173 Parsing Traditional Messages .................................................................... 176 Understanding MIME ......................................................................................... 180 Composing MIME Attachments ........................................................................ 182 Composing MIME Alternatives ...................................................................... 185 Composing Non-English Headers ................................................................. 187 Composing Nested Multiparts ...................................................................... 188 Parsing MIME Messages ................................................................................... 190 Summary ................................................................................................................. 195 Chapter 10 Simple Message Transport Protocol ............ 197 Introducing the SMTP Library .................................................................... 197 Error Handling and Conversation Debugging ....................................... 199 Getting Information from EHLO ................................................................. 202 Using Secure Sockets Layer and Transport Layer Security ......... 205 Authenticating .................................................................................................. 208 SMTP Tips ............................................................................................................. 209 Summary ................................................................................................................. 210 ix