python - requests.codes.ok 是否包含 304?

标签 python python-requests http-status-code-304

我有一个程序使用请求模块发送一个 get 请求,该请求(正确地)以 304“Not Modified”响应。发出请求后,我检查以确保 response.status_code == requests.codes.ok,但此检查失败。请求不会将 304 视为“ok”吗?

最佳答案

有一个名为 ok 的属性在返回 TrueResponse 对象中,如果状态代码不是 4xx5xx

因此您可以执行以下操作:

if response.ok:
    # 304 is included

这个属性的代码很简单:

@property
def ok(self):
    try:
        self.raise_for_status()
    except HTTPError:
        return False
    return True

关于python - requests.codes.ok 是否包含 304?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22494794/

相关文章:

python-3.x - ConnectionResetError 异常 : [Errno 54] Connection reset by peer

python - 使用自定义信息填写日历

android - 响应代码 304 和 200 时的 Volley 异常错误

python - 在 Python 中查询日期时间对象并与当前日期进行比较

python - 使用 boost-python 将 python 变量设置为 C++ 对象指针

python - 一次重载多个运算符

python - 在 python 中使用 requests_futures 进行异常处理

ruby-on-rails - 在 Rails 3.2.3 服务器中,我收到错误 304 Not Modified (5ms)

http - 为什么304状态码算作 "redirect?"

python - IPython 中的 cd 与 !cd 与 %cd