I can not use Requests in Jupyter

Hi, the error massages appear all the time while I follow the instruction in Jupyter. But it is fine in other places. It is really strange. Because I saw lots of resource that show Jupyter can run "requests". Did I get something wrong before import requests or I need to write some more code like matplotlib in Jupyter?

```
     import requests
     r = requests.get('https://www.python.org')
     r.status_code
```

> ---
> 
> gaierror                                  Traceback (most recent call last)
> /opt/conda/lib/python3.5/site-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 
> 
> /opt/conda/lib/python3.5/site-packages/requests/packages/urllib3/util/connection.py in create_connection(address, timeout, source_address, socket_options)
>      66     err = None
> ---> 67     for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
>      68         af, socktype, proto, canonname, sa = res
> 
> /opt/conda/lib/python3.5/socket.py in getaddrinfo(host, port, family, type, proto, flags)
>     731     addrlist = []
> --> 732     for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
>     733         af, socktype, proto, canonname, sa = res
> 
> gaierror: [Errno -2] Name or service not known
> 
> During handling of the above exception, another exception occurred:
> 
> NewConnectionError                        Traceback (most recent call last)
> /opt/conda/lib/python3.5/site-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 
> 
> /opt/conda/lib/python3.5/site-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 
> 
> /opt/conda/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 
> 
> /opt/conda/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 
> 
> /opt/conda/lib/python3.5/http/client.py in endheaders(self, message_body)
>    1078             raise CannotSendHeader()
> -> 1079         self._send_output(message_body)
>    1080 
> 
> /opt/conda/lib/python3.5/http/client.py in _send_output(self, message_body)
>     910 
> --> 911         self.send(msg)
>     912         if message_body is not None:
> 
> /opt/conda/lib/python3.5/http/client.py in send(self, data)
>     853             if self.auto_open:
> --> 854                 self.connect()
>     855             else:
> 
> /opt/conda/lib/python3.5/site-packages/requests/packages/urllib3/connection.py in connect(self)
>     161     def connect(self):
> --> 162         conn = self._new_conn()
>     163         self._prepare_conn(conn)
> 
> /opt/conda/lib/python3.5/site-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 0x7fee369df7f0>: Failed to establish a new connection: [Errno -2] Name or service not known
> 
> During handling of the above exception, another exception occurred:
> 
> MaxRetryError                             Traceback (most recent call last)
> /opt/conda/lib/python3.5/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
>     375                     retries=self.max_retries,
> --> 376                     timeout=timeout
>     377                 )
> 
> /opt/conda/lib/python3.5/site-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()
> 
> /opt/conda/lib/python3.5/site-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='www.ichangtou.com', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7fee369df7f0>: Failed to establish a new connection: [Errno -2] Name or service not known',))
> 
> During handling of the above exception, another exception occurred:
> 
> ConnectionError                           Traceback (most recent call last)
> <ipython-input-7-6f872ca7f8cc> in <module>()
>       4 headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
>       5 
> ----> 6 response = requests.get(url, headers=headers)
>       7 print(response.content)
> 
> /opt/conda/lib/python3.5/site-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 
> 
> /opt/conda/lib/python3.5/site-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 
> 
> /opt/conda/lib/python3.5/site-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
> 
> /opt/conda/lib/python3.5/site-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)
> 
> /opt/conda/lib/python3.5/site-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='www.ichangtou.com', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7fee369df7f0>: Failed to establish a new connection: [Errno -2] Name or service not known',))


The problem here is that the machine that is running the Jupyter Python kernel is not able to DNS resolve the hostname `www.ichangtou.com`. Do you have physical access to the machine running the kernel?


Hi Lukasa, I don't think I have the physical access. I just open the website and write the code. And any other idea can help?


@hedgeliu I'm afraid not. you can try connecting via IP address directly, but if you don't have access to the machine it's highly likely that your hosting provider doesn't allow you to perform network access at all. You'd have to run the notebook locally.


Sorry to raise again, Lukasa. I had tried in different places and laptops between Mainland china and Hongkong lots of time. But it still not work. Thus I think it is not the machine problem. @Lukasa 


@hedgeliu Did you try running the notebook server on lots of different laptops, or just running the notebook inside the web page? Because the notebook server is actually where the code executes, not the web page.


I have the same issue, not only with requests, but any other library trying to make a connection to any rest api. The issue doesn't manifest itself when I use the same code on the same machine outside of Jupyter or using curl. In my case it's not a DNS issue. I'm going to run some packet captures and see if that can tell me anything. Unfortunately I don't have a resolution, but just wanted to call out that I am seeing the same behavior and have ruled out DNS resolution on my machine as a root cause.