Response objects

Let's explore the response object in detail. We can see in the previous example that urlopen () returns an instance of the http.client.HTTPResponse class. The response object returns information about the requested resource data, and the properties and metadata of the response.

The following code makes a simple request with urllib2:

>>> response = urllib2.urlopen('http://www.python.org')>>> response.read()b'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">\n<html>>> response.read(100)

The read() method allows us to read the requested resource data and return the specified number of bytes.

Get Mastering Python for Networking and Security now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.