Python使用requests時遇到Failed to establish a new connection


再寫Zeppelin的CLI工具的時候
https://github.com/del680202/zdairi

遇到了開起太多connection這樣一個錯誤

requests.exceptions.ConnectionError: HTTPConnectionPool(host='xxxxx', port=xxxxx): Max retries exceeded with url: /api/notebook/2BG5CTGN7/paragraph/20160407-173136_827952200 (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x29b44d0>: Failed to establish a new connection: [Errno 99] Cannot assign requested address',))

細查了一下原因出在我是用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'}) 

這邊做個筆記


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM