March 2019
Beginner
490 pages
12h 40m
English
Now, we are going to write our Python script using the urllib module. For this, create a text file called RFC_download_urllib.py:
#!/usr/bin/env python3import sys, urllib.requesttry: rfc_number = int(sys.argv[1])except (IndexError, ValueError): print('Must supply an RFC number as first argument') sys.exit(2)template = 'http://www.rfc-editor.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_download_urllib.py 2324
This is the output of the previous script, where we can see the RFC description document:
First, we import our modules and check whether an RFC number ...
Read now
Unlock full access