Python Requests:响应对象不包含'status'头部

5 投票
1 回答
2941 浏览
提问于 2025-04-17 17:31

这是在使用 Requests 1.1.0 和 Python 2.6.4(在 Python 2.7.2 上也有同样的情况)。

>>> import requests
>>> response = requests.get('http://www.google.com')
>>> response.status_code
200
>>> print response.headers.get('status')
None

根据文档,应该有一个 headers['status'] 的条目,内容像 "200 OK" 这样的字符串。

这是 headers 字典的完整内容:

>>> response.headers 
{'x-xss-protection': '1; mode=block', 'transfer-encoding': 'chunked', 'set-cookie': 'PREF=ID=74b29ee465454efd:FF=0:TM=1362094463:LM=1362094463:S=Xa96iJQX_9BrC-Vm; expires=Sat, 28-Feb-2015 23:34:23 GMT; path=/; domain=.google.com, NID=67=IH21bLPTK2gLTHCyDCMEs3oN5g1uMV99U4Wsc2YA00AbFt4fQCoywQNEQU0pR6VuaNhhQGFCsqdr0FnWbPcym-pizo0xVuS6WBJ9EOTeSFARpzrsiHh6HNnaQeCnxCSH; expires=Fri, 30-Aug-2013 23:34:23 GMT; path=/; domain=.google.com; HttpOnly', 'expires': '-1', 'server': 'gws', 'cache-control': 'private, max-age=0', 'date': 'Thu, 28 Feb 2013 23:34:23 GMT', 'p3p': 'CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."', 'content-type': 'text/html; charset=ISO-8859-1', 'x-frame-options': 'SAMEORIGIN'}

这里是我了解到这个字典应该包含 'status' 条目的地方。

我是不是做错了什么?

1 个回答

4

你在寻找“原因”

>>> x=requests.get("http://apple.adam.gs")
>>> x.reason
'OK'
>>> 

custom.php 文件包含:

header("HTTP/1.1 200 Testing")

结果是:

>>> x=requests.get("http://apple.adam.gs/custom.php")
>>> print x.reason
Testing
>>> 

撰写回答