0

I have checked, socket.error:[errno 99] cannot assign requested address and namespace in python

Repeated POST request is causing error "socket.error: (99, 'Cannot assign requested address')"

requests, cannot assign requested address, out of ports?

but none of the questions gives an expected answer in case my problem. I have the following code snippet:

    with requests.Session() as session:
        js_response = session.get(url).json()
  • Note: I tried with urllib2, and simply using request.get() as well ! I am trying to query the url repeatedly, and expect a JSON object each time. But, after some requests are done, I start getting the Error 99 (marked below). This is not a continuous process. The errors keep coming intermittently. I understand that the error occurs because I am running out of the requesting ports, and Linux kernel is not able to allocate more ports for the outgoing requests, and thus throws an error.

Error:

('Connection aborted.', error(99, 'Cannot assign requested address')) 

Could anyone kindly suggest me a way in Python, which will help me to request the URL repeatedly, without getting the aforementioned error.

Community
  • 1
  • 1
ATP
  • 832
  • 1
  • 15
  • 29

1 Answers1

2

The reason that I was getting this issue was that, the number of ephemeral ports that are available by default in Linux are limited. Additionally, the ports are not readily made available once their earlier owner releases the control, because of the way TCP is designed to work. It waits for some time before allocating the port number to some other process.

So, what happens exactly is that, my Python script, tries to use all the available ports and goes on releasing them after use, but as per the TCP implementation, the port is not immediately available for use by some other process/ other request, and thus I see the error message.

More about this is available at my other question, and its responses on the Unix and Linux StackExachange forum: https://unix.stackexchange.com/questions/211647/how-safe-is-it-to-change-the-linux-ephemeral-port-range

Community
  • 1
  • 1
ATP
  • 832
  • 1
  • 15
  • 29