Learning Python Network Programming

(Sean Pound) #1
Chapter 1
except (IndexError, ValueError):
print('Must supply an RFC number as first argument')
sys.exit(2)

template = 'http://www.ietf.org/rfc/rfc{}.txt'
url = template.format(rfc_number)
rfc_raw = urllib.request.urlopen(url).read()
rfc = rfc_raw.decode()
print(rfc)

We can run the preceding code by using the following command:


$ python RFC_downloader.py 2324 | less


On Windows, you'll need to use more instead of less. RFCs can run to many
pages, hence we use a pager here. If you try this, then you should see some useful
information on the remote control of coffee pots.


Let's go through our code and look at what we've done so far.


First, we import our modules and check whether an RFC number has been supplied
on the command line. Then, we construct our URL by substituting the supplied RFC
number. Next, the main activity, the urlopen() call will construct an HTTP request
for our URL, and then it will contact the IETF web server over the Internet and
download the RFC text. Next, we decode the text to Unicode, and finally we print
it out to screen.


So, we can easily view any RFC that we like from the command line. In retrospect,
it's not entirely surprising that there isn't a module for this, because we can use
urllib to do most of the hard work!


Looking deeper


But, what if HTTP was brand new and there were no modules, such as urllib,
which we could use to speak HTTP for us? Well, then we would have to step down
the stack again and use TCP for our purposes. Let's modify our program according
to this scenario, as follows:


import sys, socket

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