Santhosh
Santhosh

Reputation: 11774

Requests: how to respond for a specific exception

When i run

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)

I see

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)'))

Now how to handle this specific exeception Max retries exceeded with url

Because this string is provided by the host. Will this fall into any sepecific exception in python also, so that i can handle it. Or the only way is to match the string of exception with Max retries exceeded with url

Upvotes: 0

Views: 530

Answers (1)

Tairong Ggomee
Tairong Ggomee

Reputation: 85

With standard Python's exception usage,

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

Also, with your provided code, the url need to have https:// to be a valid schema.

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

Upvotes: 1

Related Questions