在 `with` 语句中使用 requests.post() 时出现 AttributeError

kap*_*101 6 python python-requests

A/C python 请求文档,with 语句可以与请求一起使用以提高速度。

with requests.get(' http://httpbin.org/get ', stream=True) as r: # 在这里处理响应。

那么为什么这会返回“属性错误”呢?

Traceback (most recent call last):
File "<pyshell#101>", line 1, in <module>
with requests.post(url,headers=headers,data=data,stream=True) as post_res:
AttributeError: __exit__
Run Code Online (Sandbox Code Playgroud)

代码:

with requests.post(url,headers=headers,data=data,stream=True) as post_res:
    print(b'Name' in post_res.content)
Run Code Online (Sandbox Code Playgroud)

PS 这在没有“with”语句的情况下工作正常。

use*_*968 1

AFAICS 上下文管理器仅针对GET请求进行记录,而不针对POST. 这确实有道理,因为POST无论如何都不是幂等的。