Python:requests.exceptions.ConnectionError:HTTPConnectionPool(),URL超过最大重试次数

时间:2019-11-08 09:08:52

标签: python web-crawler

我使用python3请求模块访问我公司的api。几千次请求后(我需要一百万次请求),遇到问题:

Traceback (most recent call last):
  File "C:\Users\xxxxxx\Anaconda3\lib\site-packages\urllib3\connection.py", line 141, in _new_conn
    (self.host, self.port), self.timeout, **extra_kw)
  File "C:\Users\xxxxxx\Anaconda3\lib\site-packages\urllib3\util\connection.py", line 83, in create_connection
    raise err
  File "C:\Users\xxxxxx\Anaconda3\lib\site-packages\urllib3\util\connection.py", line 73, in create_connection
    sock.connect(sa)
TimeoutError: [WinError 10060] xxxxxx.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\xxxxxx\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 601, in urlopen
    chunked=chunked)
  File "C:\Users\xxxxxx\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 357, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "C:\Users\xxxxxx\Anaconda3\lib\http\client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Users\xxxxxx\Anaconda3\lib\http\client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Users\xxxxxx\Anaconda3\lib\http\client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Users\xxxxxx\Anaconda3\lib\http\client.py", line 1026, in _send_output
    self.send(msg)
  File "C:\Users\xxxxxx\Anaconda3\lib\http\client.py", line 964, in send
    self.connect()
  File "C:\Users\xxxxxx\Anaconda3\lib\site-packages\urllib3\connection.py", line 166, in connect
    conn = self._new_conn()
  File "C:\Users\xxxxxx\Anaconda3\lib\site-packages\urllib3\connection.py", line 150, in _new_conn
    self, "Failed to establish a new connection: %s" % e)
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x00000252463B7588>: Failed to establish a new connection: [WinError 10060] xxxxxx
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\xxxxxx\Anaconda3\lib\site-packages\requests\adapters.py", line 440, in send
    timeout=timeout
  File "C:\Users\xxxxxx\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 639, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "C:\Users\xxxxxx\Anaconda3\lib\site-packages\urllib3\util\retry.py", line 388, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='uf-api6.ksord.com', port=80): Max retries exceeded with url: /xxxxxx/get/ (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x00000252463B7588>: Failed to establish a new connection: [WinError 10060] xxxxxx',))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "D:/pyworkspace/pycharm/eb/analysis.py", line 141, in <module>
    local_req()
  File "D:/pyworkspace/pycharm/eb/analysis.py", line 97, in local_req
    res_api = request_pika(hdid, label)
  File "D:\pyworkspace\pycharm\eb\es_and_pika_utils.py", line 21, in request_pika
    res = requests.post(url, json=data, headers={"Content-Type": "application/json", 'Connection': 'close'})
  File "C:\Users\xxxxxx\Anaconda3\lib\site-packages\requests\api.py", line 112, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "C:\Users\xxxxxx\Anaconda3\lib\site-packages\requests\api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\xxxxxx\Anaconda3\lib\site-packages\requests\sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\xxxxxx\Anaconda3\lib\site-packages\requests\sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\xxxxxx\Anaconda3\lib\site-packages\requests\adapters.py", line 508, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='api.xxxxxx.com', port=80): Max retries exceeded with url: /v2/feature/get/ (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x00000252463B7588>: Failed to establish a new connection: [WinError 10060] xxxxxx',))

这是我的代码:

res = requests.post(url, json=data, headers={"Content-Type": "application/json", 'Connection': 'close'})
res.connection.close()
res.close()

1,在上面的代码中,我关闭了每个连接,并且不让它保持活动状态。 2,然后将TcpTimedWaitDelay中的HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters调整为10。 3,我不需要代理,因为api不禁止相同的IP。 4.我的电脑内存足够。 如何解决?

0 个答案:

没有答案