Forums

ConnectionRefusedError in IPython notebooks

Hi, I'm on a paid plan and trying to run the following code from an IPython notebook:

import requests
requests.get('http://bbc.co.uk')

If I run this code from a regular Python shell it seems to be working fine. Upgraded to the paid plan only a couple of days ago, maybe that's part of the problem in that something hasn't updated yet.

Here's the full stack trace I get when I try to run this from a notebook:

ConnectionRefusedError                    Traceback (most recent call last)
/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/connection.py in _new_conn(self)
    136             conn = connection.create_connection(
--> 137                 (self.host, self.port), self.timeout, **extra_kw)
    138

/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/util/connection.py in create_connection(address, timeout, source_address, socket_options)
     90     if err is not None:
---> 91         raise err
     92

/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/util/connection.py in create_connection(address, timeout, source_address, socket_options)
     80                 sock.bind(source_address)
---> 81             sock.connect(sa)
     82             return sock

ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

NewConnectionError                        Traceback (most recent call last)
/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, **response_kw)
    558                                                   timeout=timeout_obj,
--> 559                                                   body=body, headers=headers)
    560

/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, **httplib_request_kw)
    352         # urllib3.request. It also calls makefile (recv) on the socket.
--> 353         conn.request(method, url, **httplib_request_kw)
    354

/usr/lib/python3.5/http/client.py in request(self, method, url, body, headers)
   1082         """Send a complete request to the server."""
-> 1083         self._send_request(method, url, body, headers)
   1084

/usr/lib/python3.5/http/client.py in _send_request(self, method, url, body, headers)
   1127             body = body.encode('iso-8859-1')
-> 1128         self.endheaders(body)
   1129

/usr/lib/python3.5/http/client.py in endheaders(self, message_body)
   1078             raise CannotSendHeader()
-> 1079         self._send_output(message_body)
   1080

/usr/lib/python3.5/http/client.py in _send_output(self, message_body)
    910 
--> 911         self.send(msg)
    912         if message_body is not None:

/usr/lib/python3.5/http/client.py in send(self, data)
    853             if self.auto_open:
--> 854                 self.connect()
    855             else:

/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/connection.py in connect(self)
    161     def connect(self):
--> 162         conn = self._new_conn()
    163         self._prepare_conn(conn)

/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/connection.py in _new_conn(self)
    145             raise NewConnectionError(
--> 146                 self, "Failed to establish a new connection: %s" % e)
    147

NewConnectionError: <requests.packages.urllib3.connection.HTTPConnection object at 0x7fa2c0de3f98>: Failed to establish a new connection: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

MaxRetryError                             Traceback (most recent call last)
/usr/local/lib/python3.5/dist-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    375                     retries=self.max_retries,
--> 376                     timeout=timeout
    377                 )

/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, **response_kw)
    608             retries = retries.increment(method, url, error=e, _pool=self,
--> 609                                         _stacktrace=sys.exc_info()[2])
    610             retries.sleep()

/usr/local/lib/python3.5/dist-packages/requests/packages/urllib3/util/retry.py in increment(self, method, url, response, error, _pool, _stacktrace)
    272         if new_retry.is_exhausted():
--> 273             raise MaxRetryError(_pool, url, error or ResponseError(cause))
    274

MaxRetryError: HTTPConnectionPool(host='bbc.co.uk', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7fa2c0de3f98>: Failed to establish a new connection: [Errno 111] Connection refused',))

During handling of the above exception, another exception occurred:

ConnectionError                           Traceback (most recent call last)
<ipython-input-12-d420dd58275d> in <module>()
      1 import requests
      2 
----> 3 requests.get('http://bbc.co.uk')

/usr/local/lib/python3.5/dist-packages/requests/api.py in get(url, params, **kwargs)
     65 
     66     kwargs.setdefault('allow_redirects', True)
---> 67     return request('get', url, params=params, **kwargs)
     68 
     69

/usr/local/lib/python3.5/dist-packages/requests/api.py in request(method, url, **kwargs)
     51     # cases, and look like a memory leak in others.
     52     with sessions.Session() as session:
---> 53         return session.request(method=method, url=url, **kwargs)
     54 
     55

/usr/local/lib/python3.5/dist-packages/requests/sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
    466         }
    467         send_kwargs.update(settings)
--> 468         resp = self.send(prep, **send_kwargs)
    469 
    470         return resp

/usr/local/lib/python3.5/dist-packages/requests/sessions.py in send(self, request, **kwargs)
    574 
    575         # Send the request
--> 576         r = adapter.send(request, **kwargs)
    577 
    578         # Total elapsed time of the request (approximately)

/usr/local/lib/python3.5/dist-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    435                 raise RetryError(e, request=request)
    436 
--> 437             raise ConnectionError(e, request=request)
    438 
    439         except ClosedPoolError as e:

ConnectionError: HTTPConnectionPool(host='bbc.co.uk', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7fa2c0de3f98>: Failed to establish a new connection: [Errno 111] Connection refused',))

Hm. Try killing all your currently running notebook kernels (you'll find them in the "process list" on the consoles page) and then trying again?

Thanks for having a look. Killed all processes and tried again. Same error message. Please feel free to kill any processes that are currently running if you want to do more digging.

Try connecting to a different host? Like google or facebook or similar?

also can you do a

print(os.environ.get('http_proxy'))

?

Same error with google.com.

print(os.environ.get('http_proxy'))

gives me

None

Could you try again now?

Sorry, I meant "try again" as in try to connect to an external host again -- not get the environment variable again :-)

It's working for me now. Thanks for your help, guys!

Excellent -- glad we could help!