Python:requests.exceptions.ConnectionError-每个套接字地址(协议/网络地址/端口)仅允许使用一种 [英] Python: requests.exceptions.ConnectionError - Only one usage of each socket address (protocol/network address/port) is normally permitted

查看:76
本文介绍了Python:requests.exceptions.ConnectionError-每个套接字地址(协议/网络地址/端口)仅允许使用一种的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将GET请求发送到循环内的json API,如下所示:

I'm sending GET requests to a json API inside a loop, like this:

import requests

for var in my_range:
    url = f"http://127.0.0.1:8081/api/my_query/{var}"
        response = requests.get(url=url)
        if response.status_code == 200:
            < do stuff >

我能够发送236个请求/秒,我的脚本运行了几分钟,但是我认为我的所有套接字在TIME_WAIT状态到期之前就已经绑定了,并且可以重用.

I'm able to send ~ 236 requests / s, and my script runs for a few minutes, however I think all of my sockets are binding up before the TIME_WAIT state expires and they become available for reuse.

requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=8081): Max retries exceeded with url: /api/my_query/{iter} (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x03E4BCD0>: Failed to establish a new connection: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted'))

我看到我可以降低答案从套接字模块调用 SO_REUSEADDR ,但是我找不到与该请求模块一起使用该解决方案的任何信息.在循环中的每个请求之后,我都尝试了 response.close(),但错误仍然存​​在.

I see that I can lower the TIME_WAIT state from 240 s to 30 s, however apparently that can cause other undesirable issues. I also see an answer calling on SO_REUSEADDR from the sockets module but I couldn't find anything about using that solution in conjunction with the requests module. I tried response.close() after each request in the loop but the error persists.

想知道如何修改我的代码来解决此问题?

Wondering how to modify my code to solve this issue?

推荐答案

感谢林(James Lin)和弗兰克·耶林(Frank Yellin)的提示!

Thanks to James Lin and Frank Yellin for the tip!

import requests

with requests.Session() as s:
    for var in my_range:
        url = f"http://127.0.0.1:8081/api/my_query/{var}"
            response = s.get(url=url)
            if response.status_code == 200:
                < do stuff >

问题解决了

这篇关于Python:requests.exceptions.ConnectionError-每个套接字地址(协议/网络地址/端口)仅允许使用一种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆