簡體   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