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