Reading headers from urllib.request.urlopen
up vote
0
down vote
favorite
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?
python-3.x http-headers urllib
add a comment |
up vote
0
down vote
favorite
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?
python-3.x http-headers urllib
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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?
python-3.x http-headers urllib
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?
python-3.x http-headers urllib
python-3.x http-headers urllib
edited Nov 9 at 20:00
asked Nov 9 at 19:54
dtgq
1,15121639
1,15121639
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
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.
add a comment |
up vote
0
down vote
r.info()
returns a HTTPMessage
object which is implemented using the email.message.Message
class. From the documentation it looks like headers.get('Content-Disposition')
is the method you want.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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.
add a comment |
up vote
1
down vote
accepted
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.
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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.
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.
edited Nov 9 at 20:59
answered Nov 9 at 20:46
Strigo
964
964
add a comment |
add a comment |
up vote
0
down vote
r.info()
returns a HTTPMessage
object which is implemented using the email.message.Message
class. From the documentation it looks like headers.get('Content-Disposition')
is the method you want.
add a comment |
up vote
0
down vote
r.info()
returns a HTTPMessage
object which is implemented using the email.message.Message
class. From the documentation it looks like headers.get('Content-Disposition')
is the method you want.
add a comment |
up vote
0
down vote
up vote
0
down vote
r.info()
returns a HTTPMessage
object which is implemented using the email.message.Message
class. From the documentation it looks like headers.get('Content-Disposition')
is the method you want.
r.info()
returns a HTTPMessage
object which is implemented using the email.message.Message
class. From the documentation it looks like headers.get('Content-Disposition')
is the method you want.
answered Nov 9 at 20:38
wilkben
415
415
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232458%2freading-headers-from-urllib-request-urlopen%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown