PacktLib: Python Network Programming Cookbook

Python Network Programming Cookbook


About the Author

About the Reviewers


Sockets, IPv4, and Simple Client/Server Programming


Printing your machine's name and IPv4 address

Retrieving a remote machine's IP address

Converting an IPv4 address to different formats

Finding a service name, given the port and protocol

Converting integers to and from host to network byte order

Setting and getting the default socket timeout

Handling socket errors gracefully

Modifying socket's send/receive buffer sizes

Changing a socket to the blocking/non-blocking mode

Reusing socket addresses

Printing the current time from the Internet time server

Writing a SNTP client

Writing a simple echo client/server application

Multiplexing Socket I/O for Better Performance


Using ForkingMixIn in your socket server applications

Using ThreadingMixIn in your socket server applications

Writing a chat server using

Multiplexing a web server using select.epoll

Multiplexing an echo server using Diesel concurrent library

IPv6, Unix Domain Sockets, and Network Interfaces


Forwarding a local port to a remote host

Pinging hosts on the network with ICMP

Waiting for a remote network service

Enumerating interfaces on your machine

Finding the IP address for a specific interface on your machine

Finding whether an interface is up on your machine

Detecting inactive machines on your network

Performing a basic IPC using connected sockets (socketpair)

Performing IPC using Unix domain sockets

Finding out if your Python supports IPv6 sockets

Extracting an IPv6 prefix from an IPv6 address

Writing an IPv6 echo client/server

Programming with HTTP for the Internet


Downloading data from an HTTP server

Serving HTTP requests from your machine

Extracting cookie information after visiting a website

Submitting web forms

Sending web requests through a proxy server

Checking whether a web page exists with the HEAD request

Spoofing Mozilla Firefox in your client code

Saving bandwidth in web requests with the HTTP compression

Writing an HTTP fail-over client with resume and partial downloading

Writing a simple HTTPS server code with Python and OpenSSL

E-mail Protocols, FTP, and CGI Programming


Listing the files in a remote FTP server

Uploading a local file to a remote FTP server

E-mailing your current working directory as a compressed ZIP file

Downloading your Google e-mail with POP3

Checking your remote e-mail with IMAP

Sending an e-mail with an attachment via Gmail SMTP server

Writing a guestbook for your (Python-based) web server with CGI

Screen-scraping and Other Practical Applications


Searching for business addresses using the Google Maps API

Searching for geographic coordinates using the Google Maps URL

Searching for an article in Wikipedia

Searching for Google stock quote

Searching for a source code repository at GitHub

Reading news feed from BBC

Crawling links present in a web page

Programming Across Machine Boundaries


Executing a remote shell command using telnet

Copying a file to a remote machine by SFTP

Printing a remote machine's CPU information

Installing a Python package remotely

Running a MySQL command remotely

Transferring files to a remote machine over SSH

Configuring Apache remotely to host a website

Working with Web Services – XML-RPC, SOAP, and REST


Querying a local XML-RPC server

Writing a multithreaded multicall XML-RPC server

Running an XML-RPC server with a basic HTTP authentication

Collecting some photo information from Flickr using REST

Searching for SOAP methods from an Amazon S3 web service

Searching Google for custom information

Searching Amazon for books through product search API

Network Monitoring and Security


Sniffing packets on your network

Saving packets in the pcap format using the pcap dumper

Adding an extra header in HTTP packets

Scanning the ports of a remote host

Customizing the IP address of a packet

Replaying traffic by reading from a saved pcap file

Scanning the broadcast of packets