davidtgq
davidtgq

Reputation: 4000

Reading headers from urllib.request.urlopen

I've searched for and found many answers, unfortunately all Python2-related, which looks something like this:

r = urllib.urlopen(url)
headers = r.info()
print(headers.getheader('Content-Disposition'))

However this doesn't seem to work with Python3. There is no .getheader() method. All the header data is inside r.info()._headers as a list of tuples. The underscore may suggest that this isn't to be accessed directly, or there's a more "proper" way of reading the headers... if so, what is the proper way of reading the headers?

Upvotes: 1

Views: 5608

Answers (1)

Strigo
Strigo

Reputation: 106

If url uses http or https scheme r is of type http.client.HTTPResponse. You can get headers that way:

import urllib.request

r = urllib.request.urlopen(url)
print(r.getheaders())
print(r.getheader('Content-Disposition'))

You can use print(dir(r)) to list attributes of r.

Upvotes: 2

Related Questions