# Max retries exceeded with url: XXX NewConnectionError Failed to establish a new connection

**URL:** <http://forum.companieshouse.gov.uk/t/max-retries-exceeded-with-url-xxx-newconnectionerror-failed-to-establish-a-new-connection/2426>\
**Category:** API Issues\
**Created:** [May 20, 2019, 6:39am UTC](http://forum.companieshouse.gov.uk/t/max-retries-exceeded-with-url-xxx-newconnectionerror-failed-to-establish-a-new-connection/2426 "2019-05-20T06:39:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabriele\_simeone](http://forum.companieshouse.gov.uk/letter_avatar_proxy/v4/letter/g/278dde/32.png) [@gabriele\_simeone](http://forum.companieshouse.gov.uk/u/gabriele_simeone)\
**Post date:** [May 20, 2019, 6:39am UTC](http://forum.companieshouse.gov.uk/t/max-retries-exceeded-with-url-xxx-newconnectionerror-failed-to-establish-a-new-connection/2426/1 "2019-05-20T06:39:50Z")

</div>

Hi, I am getting the following error in my application to get data from a list of company numbers. Anyone knows how to solve this?

```
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.companieshouse.gov.uk', port=443): Max retries exceeded with url: 
/officers/8d_bnTiwfxh8JIr3YfuwkmkWkCg/appointments?items_per_page=50&start_index=76450 
(Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7fd44506b6d8>: 
Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',))

```

Thanks

---

<div class="post-metadata">

**Author:** ![andrewbreen](http://forum.companieshouse.gov.uk/letter_avatar_proxy/v4/letter/a/cab0a1/32.png) [@andrewbreen](http://forum.companieshouse.gov.uk/u/andrewbreen)\
**Post date:** [May 20, 2019, 8:05am UTC](http://forum.companieshouse.gov.uk/t/max-retries-exceeded-with-url-xxx-newconnectionerror-failed-to-establish-a-new-connection/2426/2 "2019-05-20T08:05:05Z")

</div>

If has just started (from Thursday morning about 10am) check you aren’t communicating with TLS 1.0 - it was disabled last week. I got caught out and had to fix it.

---

<div class="post-metadata">

**Author:** ![gabriele\_simeone](http://forum.companieshouse.gov.uk/letter_avatar_proxy/v4/letter/g/278dde/32.png) [@gabriele\_simeone](http://forum.companieshouse.gov.uk/u/gabriele_simeone)\
**Post date:** [May 20, 2019, 2:18pm UTC](http://forum.companieshouse.gov.uk/t/max-retries-exceeded-with-url-xxx-newconnectionerror-failed-to-establish-a-new-connection/2426/3 "2019-05-20T14:18:14Z")

</div>

Thanks!  
I solved this by decorating my function with a backoff logic.

```
import requests
from requests import ConnectionError
from ratelimit import limits, sleep_and_retry
from backoff import on_exception, expo

FIVE_MINUTES = 300 # Number of seconds in five minutes.

@sleep_and_retry # if we exceed the ratelimit imposed by @limits forces sleep until we can start again.
@on_exception(expo, ConnectionError, max_tries=10)
@limits(calls=500, period=FIVE_MINUTES)
def call_api(url, api_key):
	r = requests.get(url, auth=(api_key, ""))

	if not (r.status_code == 200 or r.status_code == 404):
		r.raise_for_status()

	elif r.status_code == 404:
		return dict({"error": "not found"})

	else:
		return r.json()
```
