Goerzen_3715 front.fm Page i Sunday, July 18, 2004 6:36 AM Foundations of Python Network Programming JOHN GOERZEN Goerzen_3715 front.fm Page ii Sunday, July 18, 2004 6:36 AM Foundations of Python Network Programming Copyright © 2004 by John Goerzen 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 (pbk): 1-59059-371-5 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Jason Gilmore Technical Reviewer: Magnus Lie Hetland Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Tony Davis, John Franklin, Jason Gilmore, Chris Mills, Steve Rycroft, Dominic Shakeshaft, Jim Sumser, Karen Watterson, Gavin Wray, John Zukowski Project Manager: Beth Christmas Copy Edit Manager: Nicole LeClerc Copy Editor: Mark Nigara Production Manager: Kari 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 [email protected], or visit http://www.springer-ny.com. Outside the United States: fax +49 6221 345229, e-mail [email protected], or visit http://www.springer.de. For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://www.apress.com. 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. The source code for this book is available to readers at http://www.apress.com in the Downloads section. 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 ofthe 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 Contentsata 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 1 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 Specific 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 167 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
Description: