Explore HTTP Messages and Their Representation
In this case, it’s indicated by the forward slash to represent the root directory of the website. It means that the client is requesting the default resource from the server. Following the requested resource, you have the HTTP version being used, which is
HTTP/1.1 in this example. After the last line is the host header.
In the previous example, the message is all metadata with no body. The response does have two parts. The response starts with a status line that specifies the HTTP protocol,
HTTP/1.1, and the status
urllib.request module itself depends on the low-level
http module, which you don’t need to interact with directly. You do end up using some of the data structures that
http provides, such as
When you make a request with
urllib.request.urlopen(), you get an
HTTPResponse object in return. If you spend some time exploring
HTTPResponse object with
dir(), you can see all the different methods and properties that belong to it.
Here I’m in my code editor with
urllib_request.py file open. At the top of the file, you may still have
urlopen imported. If not, you can do so by typing
import urlopen. First you should import
pprint is pretty print. It’s another way of printing out data structures in a pretty, more formatted way. Next, you’re going to use
urlopen() and pass in our example website, which is
dir() is a built-in function in Python, so it does not need to be imported.
dir() is used to list the attributes of an object. It returns a list of all valid attributes and methods associated with the object you pass to it.
Within the object, you’ll see some key attributes, like
.code, which represents the status HTTP code;
.headers, which contains the HTTP headers as a dictionary;
.url, which is the URL that was requested; and a lot more.
This time, you can use
pass as a placeholder statement because the
with statement requires an indented block. Next, you can access all the headers using the
.headers attribute; you can do
pprint() so that you can see all these headers in an easy-to-read format, then your
05:50 You probably won’t need most of this information, but some applications do use it, such as your browser might use the headers to read the response, set cookies, and determine an appropriate cache lifetime.
You can also call
.getheaders() directly on the
HTTPResponse object, which will return the same list of tuples. And if you’re only interested in one header, say the
Server header, then you can use the singular
.getheader("Server"), or you can use the square bracket syntax on headers from
You can make a minor adjustment to our previous example, so where you have
response.headers.items(), you can just call one header. You can do this by doing
response.getheader(), and then pass in
Another option here to return a singular header would be to leave the headers that we had before, so
response.headers["Server"]. This will return back the exact same thing that you saw in the terminal previously.
It isn’t likely you’ll need to interact with the headers directly like this, but now you have the tools you need to dig deeper if that need arises. Next up, you’ll learn the importance of closing
Become a Member to join the conversation.