requests模塊tcp連接過多問題

報錯:

HTTPSConnectionPool(host='xxx.xxx.com', port=443): Max retries exceeded with url: /xxxxxxxxxxxxxxxxxx 
(Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x000001E39DDBEC88>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed',))

原因:

細查了一下原因出在我是用Python的requests套件去送HTTP Request
原本我一直以為下面這種寫法不會佔用到太多connection資源

requests.post("http....")
requests.get("http....")

但是程式邏輯的關係我會在短時間使用多次requests.post
其結果就是跳出了Failed to establish a new connection這樣一個錯誤
google一下之後,一個根本的解決方法是在發起一個http request之後設定header將其關閉

解決方法:

requests..get("http://...", headers={'Connection':'close'})
requests..post("http://...", headers={'Connection':'close'})

這邊做個筆記

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章