Requests with urllib
We have already seen some examples of HTTP exchanges while discussing the RFC downloaders in Chapter 1, Network Programming and Python. The urllib
package is broken into several submodules for dealing with the different tasks that we may need to perform when working with HTTP. For making requests and receiving responses, we employ the urllib.request
module.
Retrieving the contents of a URL is a straightforward process when done using urllib
. Load your Python interpreter and do the following:
>>> from urllib.request import urlopen >>> response = urlopen('http://www.debian.org') >>> response <http.client.HTTPResponse object at 0x7fa3c53059b0> >>> response.readline() b'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">\n'
We use the urllib.request.urlopen()
function for sending a request and receiving a response for the resource at http://www.debian.org, in this case an HTML page. We...