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

后端 未结 3 1369
-上瘾入骨i
-上瘾入骨i 2020-12-18 10:48

I have a web-service deployed in my box. I want to check the result of this service with various input. Here is the code I am using:

import sys
import httpli         


        
相关标签:
3条回答
  • 2020-12-18 11:38

    This is not a python problem.

    In linux kernel 2.4 the ephemeral port range is from 32768 through 61000. So number of available ports = 61000-32768+1 = 28233. From what i understood, because the web-service in question is quite fast (<5ms actually) thus all the ports get used up. The program has to wait for about a minute or two for the ports to close.

    What I did was to count the number of conn.close(). When the number was 28000 wait for 90sec and reset the counter.

    0 讨论(0)
  • 2020-12-18 11:42

    BIGYaN identified the problem correctly and you can verify that by calling "netstat -tn" right after the exception occurs. You will see very many connections with state "TIME_WAIT".

    The alternative to waiting for port numbers to become available again is to simply use one connection for all requests. You are not required to call conn.close() after each call of conn.request(). You can simply leave the connection open until you are done with your requests.

    0 讨论(0)
  • 2020-12-18 11:48

    I too faced similar issue while executing multiple POST statements using python's request library in Spark. To make it worse, I used multiprocessing over each executor to post to a server. So thousands of connections created in seconds that took few seconds each to change the state from TIME_WAIT and release the ports for the next set of connections.

    Out of all the available solutions available over the internet that speak of disabling keep-alive, using with request.Session() et al, I found this answer to be working which makes use of 'Connection' : 'close' configuration as header parameter. You may need to put the header content in a separte line outside the post command though.

    headers = {
            'Connection': 'close'
    }
    with requests.Session() as session:
    response = session.post('https://xx.xxx.xxx.x/xxxxxx/x', headers=headers, files=files, verify=False)
    results = response.json()
    print results
    

    This is my answer to the similar issue using the above solution.

    0 讨论(0)
提交回复
热议问题