Response objects
Let's take a closer look at our response object. We can see from the preceding example that urlopen()
returns an http.client.HTTPResponse
instance. The response object gives us access to the data of the requested resource, and the properties and the metadata of the response. To view the URL for the response that we received in the previous section, do this:
>>> response.url 'http://www.debian.org'
We get the data of the requested resource through a file-like interface using the readline()
and read()
methods. We saw the readline()
method in the previous section. This is how we use the read()
method:
>>> response = urlopen('http://www.debian.org') >>> response.read(50) b'g="en">\n<head>\n <meta http-equiv="Content-Type" c'
The read()
method returns the specified number of bytes from the data. Here it's the first 50 bytes. A call to the read()
method with no argument will return all the data in one go.
The file-like interface is limited...