




























































































Estude fácil! Tem muito documento disponível na Docsity
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Prepare-se para as provas
Estude fácil! Tem muito documento disponível na Docsity
Prepare-se para as provas com trabalhos de outros alunos como você, aqui na Docsity
Encontra documentos específicos para os exames da tua universidade
Prepare-se com as videoaulas e exercícios resolvidos criados a partir da grade da sua Universidade
Responda perguntas de provas passadas e avalie sua preparação.
Ganhe pontos para baixar
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
An overview of writing network servers in python, focusing on socket programming and mime for handling email messages. Topics include writing tcp and udp servers, setting socket options, and parsing email messages using mime. It also covers the use of various python modules such as urllib2, socket, and xmlparser.
Tipologia: Notas de estudo
1 / 520
Esta página não é visível na pré-visualização
Não perca as partes importantes!





























































































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- 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 _corn, 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.
Contents at a Glance
vi
Contents
About the Author ..........................................................................................^ xv
About the Technical Reviewer ........................................................ xvii
Acknowledgments ........................................................................................... xix
Introduction ..................................................................................................^ xxi
Part One Low-Level Networking .............................................^!
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 inPython ......................................................................................... 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 22 Asynchronous Communication .............................. 469
Index ................................................................................................................... 491
xiii
About the
Technical
Magnus Lie Hedand is an associate professor of algorithms at the Norwegian University of Science and Technology (NTNU). He has been using Python since 1997 and is the author of the popular online tutorials "Instant Hacking" and "Instant Python." His publications include Practical Python (Apress, 2004) as well as several scientific papers.
Reviewer
xvii
Acknowledgments
THIS BOOK HAS BENEFITED tremendously from the insight, experience, dedication, and encouragement of many people. Without them, this project would not have happened. I'd like to especially thank the following people:
Many people contributed to the technologies used in this book. I'd like to highlight a few of them:
xix
Introduction
TwENTY-FIVE YEARS AGO, the world was a different place. Talking to someone across the ocean was, formostpeople,anexceptionally rare experience. Sending a letter could take weeks. Listening to a shortwave newscast from a foreignlandrequired technical skill, patience,andprecise atmospheric conditions. Today, we think nothing of receivingane-mail from South Korea, checking the weatherinCalifornia,andreadingtheday's headlines inGermany-allinlessthan five minutes. Files zip across the global electronic network, allowing us to do every- thing from managing investment accounts to seeing pictures of distant relatives. And yet, despite allthathashappened in the last 25 years, (^) the Internet is stillin its infancy. It's anewtechnology, still growing. I wrote this book because the Internet is exciting. In the past few years, we've seen the rise of anentire industry that did not exist before. It's a place where inventors thrive. Andthatis what I hope you get from this book. I want this to be your lab manual-yourguide for inventing things that make the Internet better.
Organization
This book is divided into five parts. The firstpartexplains how the Internet works. You'll learn about the fundamentals oflnternet communication,and the examples provide you with the basic tools that you'll use to assemble your programs. Part Two covers web-based services and Part Three covers e-mail services. Many new technologies are a form of web or e-mail communication, and these parts explain how to write programs that take advantage of them. Part Four covers other technologies, such as databases and file transfer, that are often used behind the scenes of network programs. Your users may never know you use them, but they're still key components of your toolbox. Part Five shows you how to write servers, the applications that answer requests and give out information. You may never need to write a server, but if you're designing a new protocol from scratch, you'll need the techniques here. Finally, Part Six shows you how to do more than one thing at a time. Some network programmers never need to use these techniques. Others, such as server designers, couldn't survive without them.
xxi
Introduction
xxii
Assumptions
If you're reading this book, I assume that you already know Python. If not, I recommend the Apress book Practical Python by Magnus Lie Hetland. I also assume that you have Python 2.3 or above installed on your system with networking support compiled in. If you don't already have Python installed, check first with your operating system's packaging or installation system; a package is available for many systems. Otherwise, source code and installers can be down- loaded directly from www. python. org. Finally, I assume that you have a network connection configured on your system (even if you didn't personally configure it). You don't need to have any knowledge of Internet protocols to read this book.
Examples
There are many examples in these pages. You can download all of them from the Apress website at www. apress. com. I encourage you to try the examples included here, and to experiment with them. Many are suitable as a starting point for your own programs, and almost every one is a fully contained, ready-to-run program. You can see for yourself how things work. I'll often show you an example of an interaction with a program. When you see such an example, text in bold represents keyboard input. When you see commands, the $ character represents your operating system's command prompt. Networking relies heavily on the network support in your operating system. The operating system provides all of the basic infrastructure necessary for communicating with other computers. Here are some notes related to specific operating systems.
TIP If you have a problem with any example in this book, check these notes or the text in the chapter for an explanation. Some examples cannot run on all operating systems.
Linux, FreeBSD, Mac OS X, Solaris, and Other UNIX Platforms
Each example program assumes that you have the Python interpreter, named python, installed on your path. They also assume you have the /usr/bin/env program available. The first line of each executable Python script begins with#!,