![](/img/trans.png)
[英]How to access the current response's status_code in Flask's teardown_request?
[英]How to test the different status_code (s) in a python get request?
嗨,我開始學習 Python,我想測試獲取請求的狀態碼返回的不同可能場景。
如何強制 requests.get(url) 無法顯示不同的可能代碼場景?
例子:
獲得 200 並打印 Get 成功 如果在 400 -500 之間打印:錯誤請求 如果在 300 400 之間打印:重定向錯誤等...
到目前為止,我能夠成功 GET 並驗證代碼是 200,但我知道如何讓它在每種情況下故意失敗......
謝謝
您可以使用 try 和 except 來顯示錯誤。
try:
response = requests.get(url, json=data)
except Exception as err:
if err == "NotFound"
return err, 404
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.