I have been reading Brandon Rhodes and John Goerzen’s Foundations of Python Network Programming, Second Edition (New York: Apress, 2010). It has been — and I suspect will continue to be — a real education, introducing in systematic fashion the most important of the discrete but interrelated topics of computer networking. This subject is surely the very foundation of the Internet, and I’m finally coming to understand many things that have been mysterious to me these twenty years, since I first encountered the modern Internet.
Since the edition I have of the Rhodes-Goerzen book is approaching four years old and written for Python v. 2, I’ve been reworking their sample code for Python 3. Below are the pieces that are done — all are currently in their own repository.
- Listing 2–1.
udp_local.py
: UDP Server and Client on the Loopback Interface - Listing 2–2.
udp_remote.py
: UDP Server and Client on Different Machines - Listing 2–3.
big_sender.py
: Sending a Very Large UDP Packet (note: This program works on Ubuntu 14.04 but not on Mac OS 10.9.4;IN.IP_MTU_DISCOVER
is not found in Python 3 on OS X, and I haven't found a workaround. In such cases, the Rhodes/Goerzen version supplies a variable looked up manually in one of thein.h
headers in their version of Linux.) - Listing 2–4.
udp_broadcast.py
: UDP Broadcast - Listing 3–1.
tcp_sixteen.py
: Simple TCP Server and Client - Listing 3–2.
tcp_deadlock.py
: TCP Server and Client That Deadlock - Listing 4–1.
www_ping.py
: Usinggetaddrinfo()
to Create and Connect a Socket - Listing 4–2.
forward_reverse.py
: Confirming a Forward Lookup with a Reverse Lookup - Listing 4–3.
dns_basic.py
: A Simple DNS Query Doing Its Own Recursion - Listing 4–4.
dns_mx.py
: Resolving an E-mail Domain Name
- Listing 9–1.
verbose_handler.py
: An HTTP Request and Response that Prints All Headers
The following are the main changes that have had to be made so far:
print >>sys.stderr, <str>
=>sys.stderr.write(<str>)
print
as function- string formatting with
<str>.format()
- sockets now send and receive strings as bytestring:
- send byte string: 'content' => b'content'
- convert str variable to bytes: message => bytes(message, 'utf-8')
- after receiving, convert to str: more => str(more, 'utf-8')
- some modules have changed:
urllib2
=>urllib
, and the new module is organized differentlyio
:io.StringIO
=>io.BytesIO
because we are dealing with bytestring nowpydns
=>py3dns
[end]