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:
- url –checks for follow redirect operations.
- headers – returns the headers of the response in the EmailMessage instance.
- 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 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:
('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 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:
We can also fetch the character set directly from the response, as shown in the code snippets below:
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:
(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.