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