简体   繁体   English

在`with`语句中使用request.post()时发生AttributeError

[英]AttributeError when using requests.post() in `with` statement

A/C python requests documentation , with statement can be used with requests for better speed. A / C python请求文档 ,with语句可与请求一起使用,以提高速度。

with requests.get(' http://httpbin.org/get ', stream=True) as r: # Do things with the response here. 将request.get(' http://httpbin.org/get',stream = True)设置为r:#在此处处理响应。

So why is this returning 'Attribute error'? 那么为什么返回“属性错误”呢?

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__

code: 码:

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

PS This is working fine without 'with' statement. 附注:如果没有“ with”语句,此方法可以正常工作。

AFAICS a context manager is documented only for GET requests, not POST . AFAICS仅针对GET请求(而非POST记录了上下文管理器。 That does make sense since POST is not indempotent anyway. 这样做确实是有道理的,因为POST并不是独立的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM