python requests response location

3.6k Views Asked by At

I am trying to find a way to get the response location using python requests. When I use fiddler or use python requests the returned headers don't include the location it came from. but looking at the same request in my browser it shows the response location. anyway of doing this? thanks

2

There are 2 best solutions below

0
On

Here's an example using urllib2, but I don't think you can do it with requests:

>>> urllib2.urlopen('http://google.com').info().__dict__
{'fp': None, 'status': '', 'startofbody': None, 'startofheaders': None, 'subtype': 'html', 'type': 'text/html', 'maintype': 'text', 'headers': ['Date: Mon, 19 Dec 2016 23:11:59 GMT\r\n', 'Expires: -1\r\n', 'Cache-Control: private, max-age=0\r\n', 'Content-Type: text/html; charset=ISO-8859-1\r\n', 'P3P: CP="This is not a P3P policy! See https://www.google.com/support/accounts/answer/151657?hl=en for more info."\r\n', 'Server: gws\r\n', 'X-XSS-Protection: 1; mode=block\r\n', 'X-Frame-Options: SAMEORIGIN\r\n', 'Set-Cookie: NID=93=TKYj49Z3JkIbdrSKS3TgOIKLualPZ--E7dYEXg0S4lYvveiaGLAWjXWpy6xNrutXuTkEkaUH5u1K8Gq8jbWaG5uDENsg3deeSOWNFqCM-YCIe_TZMeu7eUj3PW5__Yzid7gpeUUiEiNa0Q; expires=Tue, 20-Jun-2017 23:11:59 GMT; path=/; domain=.google.com; HttpOnly\r\n', 'Accept-Ranges: none\r\n', 'Vary: Accept-Encoding\r\n', 'Connection: close\r\n'], 'dict': {'x-xss-protection': '1; mode=block', 'set-cookie': 'NID=93=TKYj49Z3JkIbdrSKS3TgOIKLualPZ--E7dYEXg0S4lYvveiaGLAWjXWpy6xNrutXuTkEkaUH5u1K8Gq8jbWaG5uDENsg3deeSOWNFqCM-YCIe_TZMeu7eUj3PW5__Yzid7gpeUUiEiNa0Q; expires=Tue, 20-Jun-2017 23:11:59 GMT; path=/; domain=.google.com; HttpOnly', 'accept-ranges': 'none', 'expires': '-1', 'vary': 'Accept-Encoding', 'server': 'gws', 'connection': 'close', 'cache-control': 'private, max-age=0', 'date': 'Mon, 19 Dec 2016 23:11:59 GMT', 'p3p': 'CP="This is not a P3P policy! See https://www.google.com/support/accounts/answer/151657?hl=en for more info."', 'content-type': 'text/html; charset=ISO-8859-1', 'x-frame-options': 'SAMEORIGIN'}, 'typeheader': 'text/html; charset=ISO-8859-1', 'encodingheader': None, 'seekable': 0, 'unixfrom': '', 'plisttext': '; charset=ISO-8859-1', 'plist': ['charset=ISO-8859-1']}

Here's more info in the following answer: getting value of location header using python urllib2.

0
On

This can be done with requests; had to figure it out yesterday.

r.headers['location']

Generically, print(yaml.dump(r)) showed the full response.

  • requests v2.19.1 in python 3.7