Book Image

Learning Python Network Programming

By : Dr. M. O. Faruque Sarker, Samuel B Washington, Sam Washington
Book Image

Learning Python Network Programming

By: Dr. M. O. Faruque Sarker, Samuel B Washington, Sam Washington

Overview of this book

Table of Contents (17 chapters)
Learning Python Network Programming
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

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...