0

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

1 Answer 1

1

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. ... ' 
Sign up to request clarification or add additional context in comments.

4 Comments

They may need to import urllib3 as well or they'll end up with a NameError. Or alternatively from urllib3.exceptions import MaxRetryError
Of course yes. I supposed to import urllib3 at the beginning.
Responses do have status codes. The MaxRetryError is raised when we fail to make a connection with the endpoint (i.e. timed out), not because we're being rate-limited by the server (which might send us a 429 to tell us so)
so the error is about the connection failure and requests.get(url) only tries once. So Max retries exceeded means it talking w.r.t to the requests library not w.r.t to the server.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.