请求:如何响应特定异常

2024-09-27 00:13:11 发布

您现在位置:Python中文网/ 问答频道 /正文

当我跑的时候

import requests
url = "https://api.tdameritrade.com/v1/marketdata/ARGO/pricehistory?apikey=APIKEY&periodType=month&startDate=1555790109000&endDate=1618862109000&frequencyType=weekly&frequency=1"
requests.get(url,timeout=10)

我明白了

HTTPSConnectionPool(host='api.tdameritrade.com', port=443): Max retries exceeded with url: /v1/marketdata/ARGO/pricehistory?apikey=APIKEY&periodType=month&startDate=1555790109000&endDate=1618862109000&frequencyType=weekly&frequency=1 (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x7fb6ff5610d0>, 'Connection to api.tdameritrade.com timed out. (connect timeout=10)'))
 TRACEBACK: 
Traceback (most recent call last):
  File "/home/simha/app/.venv/lib/python3.7/site-packages/urllib3/connection.py", line 170, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw
  File "/home/simha/app/.venv/lib/python3.7/site-packages/urllib3/util/connection.py", line 96, in create_connection
    raise err
  File "/home/simha/app/.venv/lib/python3.7/site-packages/urllib3/util/connection.py", line 86, in create_connection
    sock.connect(sa)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/simha/app/.venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 706, in urlopen
    chunked=chunked,
  File "/home/simha/app/.venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 382, in _make_request
    self._validate_conn(conn)
  File "/home/simha/app/.venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 1010, in _validate_conn
    conn.connect()
  File "/home/simha/app/.venv/lib/python3.7/site-packages/urllib3/connection.py", line 353, in connect
    conn = self._new_conn()
  File "/home/simha/app/.venv/lib/python3.7/site-packages/urllib3/connection.py", line 177, in _new_conn
    % (self.host, self.timeout),
urllib3.exceptions.ConnectTimeoutError: (<urllib3.connection.HTTPSConnection object at 0x7fb6ff5610d0>, 'Connection to api.tdameritrade.com timed out. (connect timeout=10)')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/simha/app/.venv/lib/python3.7/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/home/simha/app/.venv/lib/python3.7/site-packages/urllib3/connectionpool.py", line 756, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "/home/simha/app/.venv/lib/python3.7/site-packages/urllib3/util/retry.py", line 574, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.tdameritrade.com', port=443): Max retries exceeded with url: /v1/marketdata/ARGO/pricehistory?apikey=APIKEY&periodType=month&startDate=1555790109000&endDate=1618862109000&frequencyType=weekly&frequency=1 (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x7fb6ff5610d0>, 'Connection to api.tdameritrade.com timed out. (connect timeout=10)'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/simha/app/src/stock/cron_jobs/function_to_extract_data_only_last_100.py", line 278, in getDataEachSymbol
    historyData = requests.get(url,timeout=10)
  File "/home/simha/app/.venv/lib/python3.7/site-packages/requests/api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "/home/simha/app/.venv/lib/python3.7/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/simha/app/.venv/lib/python3.7/site-packages/requests/sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/simha/app/.venv/lib/python3.7/site-packages/requests/sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "/home/simha/app/.venv/lib/python3.7/site-packages/requests/adapters.py", line 504, in send
    raise ConnectTimeout(e, request=request)
requests.exceptions.ConnectTimeout: HTTPSConnectionPool(host='api.tdameritrade.com', port=443): Max retries exceeded with url: /v1/marketdata/ARGO/pricehistory?apikey=APIKEY&periodType=month&startDate=1555790109000&endDate=1618862109000&frequencyType=weekly&frequency=1 (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x7fb6ff5610d0>, 'Connection to api.tdameritrade.com timed out. (connect timeout=10)'))

现在如何处理这个特定的异常Max retries exceeded with url

因为这个字符串是由主机提供的。这也会落入python中的任何单独异常中,以便我能够处理它。或者,唯一的方法是将异常字符串与Max retries exceeded with url匹配


Tags: inpyappurlhomevenvlibpackages
1条回答
网友
1楼 · 发布于 2024-09-27 00:13:11

使用标准Python的异常用法

try:
    requests.get(url,timeout=10)
except urllib3.exceptions.MaxRetryError:
    # do something

另外,使用提供的代码,url需要有https://才能成为有效的模式

url = 'https://api. ... '

相关问题 更多 >

    热门问题