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'}) 

這邊做個筆記

猜你喜欢

转载自www.cnblogs.com/ExMan/p/12163231.html