繁体   English   中英

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

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

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

将request.get(' http://httpbin.org/get',stream = True)设置为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__

码:

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

附注:如果没有“ with”语句,此方法可以正常工作。

AFAICS仅针对GET请求(而非POST记录了上下文管理器。 这样做确实是有道理的,因为POST并不是独立的。

暂无
暂无

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

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