python请求响应位置

时间:2016-12-19 22:38:16

标签: python python-requests fiddler

我正在尝试使用python请求找到获取响应位置的方法。当我使用fiddler或使用python请求时,返回的标头不包含它来自的位置。但在浏览器中查看相同的请求会显示响应位置。无论如何这样做? 感谢

2 个答案:

答案 0 :(得分:2)

这可以通过请求来完成;不得不昨天弄清楚。

r.headers['location']

通常,print(yaml.dump(r))显示了完整的答复。

  • 在python 3.7中请求v2.19.1

答案 1 :(得分:0)

以下是使用urllib2的示例,但我认为您无法使用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']}

以下答案中提供了更多信息:getting value of location header using python urllib2