Python Network Programming

[Pages:124]Python Network Programming

David M. Beazley

Edition: Thu Jun 17 19:49:58 2010

Copyright (C) 2010 David M Beazley All Rights Reserved

Python Network Programming : Table of Contents

!

1. Network Fundamentals !

!

!

!

!

!

4

!

2. Client Programming! !

!

!

!

!

!

32

!

3. Internet Data Handling! !

!

!

!

!

!

49

!

4. Web Programming Basics! !

!

!

!

!

65

!

5. Advanced Networks! !

!

!

!

!

!

93

Edition: Thu Jun 17 19:49:58 2010

Slide Title Index

0. Introduction

Introduction

0-1

Support Files

0-2

Python Networking

0-3

This Course

0-4

Standard Library

0-5

Prerequisites

0-6

1. Network Fundamentals

Network Fundamentals The Problem Two Main Issues Network Addressing Standard Ports Using netstat Connections Client/Server Concept Request/Response Cycle Using Telnet Data Transport Sockets Socket Basics Socket Types Using a Socket TCP Client Exercise 1.1 Server Implementation TCP Server Exercise 1.2 Advanced Sockets Partial Reads/Writes Sending All Data End of Data Data Reassembly Timeouts Non-blocking Sockets Socket Options Sockets as Files Exercise 1.3 Odds and Ends UDP : Datagrams UDP Server UDP Client Unix Domain Sockets Raw Sockets Sockets and Concurrency

1-1 1-2 1-3 1-4 1-5 1-6 1-7 1-8 1-9 1-10 1-11 1-12 1-13 1-14 1-15 1-16 1-17 1-18 1-19 1-27 1-28 1-29 1-31 1-32 1-33 1-34 1-35 1-36 1-37 1-39 1-40 1-41 1-42 1-43 1-44 1-45 1-46

Threaded Server Forking Server (Unix) Asynchronous Server Utility Functions Omissions Discussion

2. Client Programming

1-50 1-51 1-52 1-53 1-54 1-55

Client Programming Overview urllib Module urllib protocols HTML Forms Web Services Parameter Encoding Sending Parameters Response Data Response Headers Response Status Exercise 2.1 urllib Limitations urllib2 Module urllib2 Example urllib2 Requests Requests with Data Request Headers urllib2 Error Handling urllib2 Openers urllib2 build_opener() Example : Login Cookies Discussion Exercise 2.2 Limitations ftplib Upload to a FTP Server httplib smtplib Exercise 2.3

3. Internet Data Handling

2-1 2-2 2-3 2-5 2-6 2-8 2-9 2-10 2-12 2-13 2-14 2-15 2-16 2-17 2-18 2-19 2-20 2-21 2-22 2-23 2-24 2-25 2-26 2-27 2-28 2-29 2-30 2-31 2-32 2-33

Internet Data Handling Overview CSV Files Parsing HTML Running a Parser HTML Example XML Parsing with SAX Brief XML Refresher SAX Parsing

3-1 3-2 3-3 3-4 3-6 3-7 3-9 3-10 3-11

Exercise 3.1 XML and ElementTree etree Parsing Basics Obtaining Elements Iterating over Elements Element Attributes Search Wildcards cElementTree Tree Modification Tree Output Iterative Parsing Exercise 3.2 JSON Sample JSON File Processing JSON Data Exercise 3.3

4. Web Programming

Web Programming Basics Introduction Overview Disclaimer HTTP Explained HTTP Client Requests HTTP Responses HTTP Protocol Content Encoding Payload Packaging Exercise 4.1 Role of Python Typical Python Tasks Content Generation Example : Page Templates Commentary Exercise 4.2 HTTP Servers A Simple Web Server Exercise 4.3 A Web Server with CGI CGI Scripting CGI Example CGI Mechanics Classic CGI Interface CGI Query Variables cgi Module CGI Responses Note on Status Codes CGI Commentary Exercise 4.4 WSGI WSGI Interface

3-13 3-14 3-15 3-17 3-18 3-19 3-20 3-22 3-23 3-24 3-25 3-28 3-29 3-30 3-31 3-32

WSGI Example WSGI Applications WSGI Environment Processing WSGI Inputs WSGI Responses WSGI Content WSGI Content Encoding WSGI Deployment WSGI and CGI Exercise 4.5 Customized HTTP Exercise 4.6 Web Frameworks Commentary

5. Advanced Networking

4-1 4-2 4-3 4-4 4-5 4-6 4-7 4-8 4-9 4-10 4-11 4-12 4-13 4-14 4-15 4-17 4-18 4-19 4-20 4-21 4-22 4-23 4-24 4-27 4-28 4-29 4-30 4-31 4-32 4-33 4-34 4-35 4-36

Advanced Networking Overview Problem with Sockets SocketServer SocketServer Example Execution Model Exercise 5.1 Big Picture Concurrent Servers Server Mixin Classes Server Subclassing Exercise 5.2 Distributed Computing Discussion XML-RPC Simple XML-RPC XML-RPC Commentary XML-RPC and Binary Exercise 5.3 Serializing Python Objects pickle Module Pickling to Strings Example Miscellaneous Comments Exercise 5.4 multiprocessing Connections Connection Use Example Commentary What about... Network Wrap-up Exercise 5.5

4-37 4-38 4-39 4-41 4-42 4-44 4-45 4-46 4-48 4-49 4-50 4-53 4-54 4-56

5-1 5-2 5-3 5-4 5-5 5-11 5-12 5-13 5-14 5-15 5-16 5-17 5-18 5-19 5-20 5-21 5-23 5-24 5-25 5-26 5-27 5-28 5-29 5-31 5-32 5-33 5-34 5-35 5-36 5-38 5-40 5-41 5-42

Section 0

Introduction

Support Files

? Course exercises:



? This zip file should be downloaded and extracted someplace on your machine

? All of your work will take place in the the "PythonNetwork" folder

Copyright (C) 2010,

1

1- 2

Python Networking

? Network programming is a major use of Python ? Python standard library has wide support for

network protocols, data encoding/decoding, and other things you need to make it work

? Writing network programs in Python tends to be substantially easier than in C/C++

Copyright (C) 2010,

1- 3

This Course

? This course focuses on the essential details of network programming that all Python programmers should probably know ? Low-level programming with sockets ? High-level client modules ? How to deal with common data encodings ? Simple web programming (HTTP) ? Simple distributed computing

Copyright (C) 2010,

2

1- 4

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download