해결)requests.exceptions.ConnectTimeout: HTTPSConnectionPool(): Max retries exceeded with url: Connection to timed out



작성시간 :





(수정됨)

에러

에러 예시

import requests
url = 'https://httpstat.us/200?sleep=2000'
with requests.get(url, timeout = 0.1) as r:
    print(r.status_code)

>> Traceback (most recent call last):
  File "c:\seolpyo\example.py", line 3, in <module>
    with requests.get(url, timeout = 0.1) as r:
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\seolpyo\python\Lib\site-packages\requests\api.py", line 73, in get
    return request("get", url, params=params, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\seolpyo\python\Lib\site-packages\requests\api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\seolpyo\python\Lib\site-packages\requests\sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\seolpyo\python\Lib\site-packages\requests\sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\seolpyo\python\Lib\site-packages\requests\adapters.py", line 507, in send
    raise ConnectTimeout(e, request=request) requests.exceptions.ConnectTimeout: HTTPSConnectionPool(host='httpstat.us', port=443): Max retries exceeded with url: /200?sleep=2000 (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x000001615EFD6B10>, 'Connection to httpstat.us timed out. (connect timeout=0.1)'))

해결방법

파이썬 requetst 모듈의 timeout 변수값을 보다 길게 설정한다.

설명

time out(시간 초과)에는 2가지가 있는데, Connection timed out과 Read timed out이다.
내가 이해한 바로는 Connection timed out은 서버로 요청을 보낼 시간이 부족할 때 생기는 에러고,
Read timed out은 서버에서 응답을 보내주지 않았을 때 생기는 에러이다.

reqeusts 모듈의 경우 Connection timed out과 Read timed out을 따로 지정하는 것이 가능한데, 다음과 같이 설정하면 된다고 한다.

import requests
url = 'https://httpstat.us/200?sleep=2000'
time_connect = 10
time_read = 10
with requests.get(url, timeout = (time_connect, time_read)) as r:
    print(r.status_code)

>> 200



태그



공유

하기






white.seolpyo.com