Learning Python Network Programming

(Sean Pound) #1

Network Programming and Python


A look through the Library Reference at https://docs.python.org doesn't seem to
show anything directly relevant to our requirement. This is not entirely surprising!


So, next we will turn to third-party modules. The Python package index, which
can be found at https://pypi.python.org, is the place where we should look for
these. Here as well, running a few searches around the theme of RFC client and RFC
download doesn't seem to reveal anything useful. The next place to look will be
Google, though again, the searches don't reveal anything promising. This is slightly
disappointing, but this is why we're learning network programming, to fill
these gaps!


There are other ways in which we may be able to find out about useful third-party
modules, including mailing lists, Python user groups, the programming Q&A site
http://stackoverflow.com, and programming textbooks.


For now, let's assume that we really can't find a module for downloading RFCs.
What next? Well, we need to think lower in the network stack. This means that we
need to identify the network protocol that we'll need to use for getting hold of the
RFCs in text format by ourselves.


The IETF landing page for RFCs is http://www.ietf.org/rfc.html, and reading
through it tell us exactly what we want to know. We can access a text version of an
RFC using a URL of the form http://www.ietf.org/rfc/rfc741.txt. The RFC
number in this case is 741. So, we can get text format of RFCs using HTTP.


Now, we need a module that can speak HTTP for us. We should look at the standard
library again. You will notice that there is, in fact, a module called http. Sounds
promising, though looking at its documentation will tell us that it's a low level
library and that something called urllib will prove to be more useful.


Now, looking at the urllib documentation, we find that it does indeed do what we
need. It downloads the target of a URL through a straightforward API. We've found
our protocol module.


Downloading an RFC


Now we can write our program. For this, create a text file called RFC_downloader.py
and save the following code to it:


import sys, urllib.request

try:
rfc_number = int(sys.argv[1])
Free download pdf