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

這邊做個筆記

  • 9
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值