-1

im currently working on a python script that executes POST requests a few thousand times to fill dummy data into a database. The POST request sends a string to our backend which fills in this string into the database.

The first 10000 requests are working fine but then a ConnectionError appears.

This is a simplified implementation of my code sqlfiller.py

import requests

url = "http://localhost:3100/api"
payload = "test"

#40 represents all business-days in two months
for i in range(40):
    #600 data entries per day
    for i in range(600):
        requests.post(url, payload)

As i mentioned, it works fine for a couple thousands POST requests. But sometime this error appears and the program crashes.

Traceback (most recent call last):
  File "C:\Users\adriand\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connection.py", line 174, in _new_conn
    conn = connection.create_connection(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\adriand\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\util\connection.py", line 95, in create_connection
    raise err
  File "C:\Users\adriand\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\util\connection.py", line 85, in create_connection
    sock.connect(sa)
OSError: [WinError 10048] Normalerweise darf jede Socketadresse (Protokoll, Netzwerkadresse oder Anschluss) nur jeweils einmal verwendet werden

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\adriand\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
                       ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\adriand\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 398, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "C:\Users\adriand\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connection.py", line 239, in request
    super(HTTPConnection, self).request(method, url, body=body, headers=headers)
  File "C:\Users\adriand\AppData\Local\Programs\Python\Python311\Lib\http\client.py", line 1282, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Users\adriand\AppData\Local\Programs\Python\Python311\Lib\http\client.py", line 1328, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Users\adriand\AppData\Local\Programs\Python\Python311\Lib\http\client.py", line 1277, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Users\adriand\AppData\Local\Programs\Python\Python311\Lib\http\client.py", line 1037, in _send_output
    self.send(msg)
  File "C:\Users\adriand\AppData\Local\Programs\Python\Python311\Lib\http\client.py", line 975, in send
    self.connect()
  File "C:\Users\adriand\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connection.py", line 205, in connect
    conn = self._new_conn()
           ^^^^^^^^^^^^^^^^
  File "C:\Users\adriand\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connection.py", line 186, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x0000020309A81250>: Failed to establish a new connection: [WinError 10048] Normalerweise darf jede Socketadresse (Protokoll, Netzwerkadresse oder Anschluss) nur jeweils einmal verwendet werden

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\adriand\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\adapters.py", line 489, in send
    resp = conn.urlopen(
           ^^^^^^^^^^^^^
  File "C:\Users\adriand\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 787, in urlopen
    retries = retries.increment(
              ^^^^^^^^^^^^^^^^^^
  File "C:\Users\adriand\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\util\retry.py", line 592, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=3100): Max retries exceeded with url: /api (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000020309A81250>: Failed to establish a new connection: [WinError 10048] Normalerweise darf jede Socketadresse (Protokoll, Netzwerkadresse oder Anschluss) nur jeweils einmal verwendet werden'))   

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Workbench\sqlfiller.py", line 55, in <module>
    requests.post(url, payload)
  File "C:\Users\adriand\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\api.py", line 115, in post
    return request("post", url, data=data, json=json, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\adriand\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\adriand\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\sessions.py", line 587, in request
    resp = self.send(prep, **send_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\adriand\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\sessions.py", line 701, in send
    r = adapter.send(request, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\adriand\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\adapters.py", line 565, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=3100): Max retries exceeded with url: /api (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000020309A81250>: Failed to establish a new connection: [WinError 10048] Normalerweise darf jede Socketadresse (Protokoll, Netzwerkadresse oder Anschluss) nur jeweils einmal verwendet werden'))

This german error line WinError 10048 translates to "Only one usage of each socket address (protocol/network address/port) is normally permitted"

Someone knows whats going on? Thanks :)

M0ttii
  • 21
  • 3
  • What are you trying to do ? recover all 40 * 600 results at same time ? can't you queue it to be gentle with the api, your script looks like you trying to attack it :D – Devyl Nov 28 '22 at 13:51
  • yes im actually sending it without a delay. I think it's not a problem because my NestJS backend handles this and queue's the requests by itself. As i said, it is working for 15000+ requests but then this error appears. – M0ttii Nov 28 '22 at 13:57
  • TCP sockets normally have a wait time after being used, so if you make thousands connections very fast, I can imagine that all (IP, port) combinations are used quite quickly. You can run `netstat -n` to see if this is the case (are there very much `TIME_WAIT` entries? If so, you should try to reuse the connection instead of creating a new one everytime. – wovano Nov 28 '22 at 14:09
  • 1
    Yes, using a session fixed it. Thank you! – M0ttii Nov 28 '22 at 14:20

1 Answers1

-1

Instead of request.post use httplib.HTTPConnection(url) with concurrency . You need to change your code,That will definitely help to solve this problem.

Shubham Shah
  • 99
  • 1
  • 1
  • 7
  • You can refer this https://stackoverflow.com/questions/2632520/what-is-the-fastest-way-to-send-100-000-http-requests-in-python – Shubham Shah Nov 28 '22 at 14:17