Python

urllib.response

One of the most influential and valuable packages in the Python ecosystem is urllib. This package provides a simple and powerful interface for making HTTP requests with Python. It is a great utility, and although it’s not included by default, it is used by millions of other packages.

The urllib package is diverse; with various functions, classes, and objects for performing multiple tasks. The essence of this tutorial is to learn about the response module.

What is the urllib.response?

The urllib.response is a module that defines the functions and classes used for request responses in a file-like interface.

Functions Defined in the urllib.response Module

The module defines a set of functions used internally by urllib.request module. The functions described in this module include:

  1. url –checks for follow redirect operations.
  2. headers – returns the headers of the response in the EmailMessage instance.
  3. status – returns the status code of the server.

Example 1

We can fetch the headers from a given request using the headers of the HTTPResponse object. An example is shown below:

from urllib.request import urlopen
from pprint import pprint
with urlopen("http://geekbits.io") as response:
    pass

pprint(response.headers.items())

The above example returns detailed header information about the response. An example output is shown below:

[('Connection', 'close'),
 ('Content-Length', '110385'),
 ('Server', 'openresty'),
 ('Content-Type', 'text/html; charset=utf-8'),
 ('Status', '200 OK'),
 ('Via', '1.1 varnish, 1.1 varnish'),
 ('Accept-Ranges', 'bytes'),
 ('Date', 'Tue, 13 Sep 2022 16:29:13 GMT'),
 ('Age', '76750'),
 ('X-Served-By', 'cache-ams21052-AMS, cache-jnb7020-JNB'),
 ('X-Cache', 'HIT, HIT'),
 ('X-Cache-Hits', '1, 1'),
 ('X-Timer', 'S1663086554.603638,VS0,VE0'),
 ('Vary', 'Accept-Encoding, Cookie'),
 ('Fastly', 'true'),
 ('Alt-Svc', 'clear')]
(base)

Although you have all the header information, you probably do not need to use all of it. For example, you can filter specific headers as shown:

from urllib.request import urlopen
from pprint import pprint
with urlopen("http://geekbits.io") as response:
    pass
pprint(response.getheader("Status"))

In these cases, the query returns the status code of the request as shown:

'200 OK'

We can also fetch the character set directly from the response, as shown in the code snippets below:

from urllib.request import urlopen
from pprint import pprint
with urlopen("http://geekbits.io") as response:
    body = response.read()
charset = response.headers.get_content_charset()
pprint(charset)

And without a doubt, the code returns the response to the character encoding:

'utf-8'
(base)

Conclusion

In this post, you learned about the urllib.response module, which defines classes and functions used by other urllib modules. You also discovered how to use various functions in the module to retrieve different information.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list