Handling no network at startup in Requests

To be honest I don't know if I'm doing something wrong, but I am unable to find a solution so here goes.

This is running on my laptop connected over wifi - no proxies.I'm on Fedora 23, running requests 2.10.0 in a Python 3.4.3 virtual env.

I have an infinite loop running requests.get's on a http url (say 'http://info.cern.ch/hypertext/WWW/TheProject.html') and sleeping. 

If I start the program with wifi ON, it works well. The gets succeed. If I turn OFF wifi on my laptop (program still running), the gets start throwing exceptions which I catch and handle. Then when I turn wifi ON again (program still running), the gets start working again.

But if I turn wifi OFF and then start the program, it doesn't work. The gets throw exceptions which I catch and handle. But then if I turn ON the wifi (program still running) the gets continue to throw exceptions. At this point I can access the url from my browser but the requests.get()s continue to fail throwing

`RequestException: HTTPConnectionPool(host='info.cern.ch', port=80): Max retries exceeded with url: /hypertext/WWW/TheProject.html (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7fe105b12cf8>: Failed to establish a new connection: [Errno -5] No address associated with hostname',))`

Best I can make out is that the HTTPConnectionPool created at startup - with wifi OFF - somehow sticks around (probably tied to the eth interface?) and continues to fail even though wifi is now working.

I have tried creating a Session in the while loop and explicitly close() ing it at the end. I've tried creating a Session outside the while loop and close() and re-creating it inside the get() exception handling. I've even tried `import requests` inside the while loop (yes I know that's a bad idea) but even that fails.

I can reproduce this behaviour from the Python REPL environment as follows:
1. Turn OFF wifi
2. Start REPL, import requests and get()

```
$ python
Python 3.4.3 (default, Mar 31 2016, 20:42:37) 
[GCC 5.3.1 20151207 (Red Hat 5.3.1-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> 
>>> 
>>> requests.get('http://google.com')
Traceback (most recent call last):
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/connection.py", line 142, in _new_conn
    (self.host, self.port), self.timeout, **extra_kw)
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/util/connection.py", line 67, in create_connection
    for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
  File "/usr/lib64/python3.4/socket.py", line 533, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -5] No address associated with hostname

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 578, in urlopen
    chunked=chunked)
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 362, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib64/python3.4/http/client.py", line 1088, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib64/python3.4/http/client.py", line 1126, in _send_request
    self.endheaders(body)
  File "/usr/lib64/python3.4/http/client.py", line 1084, in endheaders
    self._send_output(message_body)
  File "/usr/lib64/python3.4/http/client.py", line 922, in _send_output
    self.send(msg)
  File "/usr/lib64/python3.4/http/client.py", line 857, in send
    self.connect()
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/connection.py", line 167, in connect
    conn = self._new_conn()
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/connection.py", line 151, in _new_conn
    self, "Failed to establish a new connection: %s" % e)
requests.packages.urllib3.exceptions.NewConnectionError: <requests.packages.urllib3.connection.HTTPConnection object at 0x7fb41c6dcb00>: Failed to establish a new connection: [Errno -5] No address associated with hostname

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "~/venv/lib64/python3.4/site-packages/requests/adapters.py", line 403, in send
    timeout=timeout
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 623, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/util/retry.py", line 281, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
requests.packages.urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='google.com', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7fb41c6dcb00>: Failed to establish a new connection: [Errno -5] No address associated with hostname',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "~/venv/lib64/python3.4/site-packages/requests/api.py", line 71, in get
    return request('get', url, params=params, **kwargs)
  File "~/venv/lib64/python3.4/site-packages/requests/api.py", line 57, in request
    return session.request(method=method, url=url, **kwargs)
  File "~/venv/lib64/python3.4/site-packages/requests/sessions.py", line 475, in request
    resp = self.send(prep, **send_kwargs)
  File "~/venv/lib64/python3.4/site-packages/requests/sessions.py", line 585, in send
    r = adapter.send(request, **kwargs)
  File "~/venv/lib64/python3.4/site-packages/requests/adapters.py", line 467, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='google.com', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7fb41c6dcb00>: Failed to establish a new connection: [Errno -5] No address associated with hostname',))
>>> 
```
1. Turn ON wifi, google.com accessible from browser
2. Back to REPL get() errors

```
>>> requests.get('http://google.com')
Traceback (most recent call last):
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/connection.py", line 142, in _new_conn
    (self.host, self.port), self.timeout, **extra_kw)
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/util/connection.py", line 67, in create_connection
    for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
  File "/usr/lib64/python3.4/socket.py", line 533, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -5] No address associated with hostname

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 578, in urlopen
    chunked=chunked)
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 362, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib64/python3.4/http/client.py", line 1088, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib64/python3.4/http/client.py", line 1126, in _send_request
    self.endheaders(body)
  File "/usr/lib64/python3.4/http/client.py", line 1084, in endheaders
    self._send_output(message_body)
  File "/usr/lib64/python3.4/http/client.py", line 922, in _send_output
    self.send(msg)
  File "/usr/lib64/python3.4/http/client.py", line 857, in send
    self.connect()
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/connection.py", line 167, in connect
    conn = self._new_conn()
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/connection.py", line 151, in _new_conn
    self, "Failed to establish a new connection: %s" % e)
requests.packages.urllib3.exceptions.NewConnectionError: <requests.packages.urllib3.connection.HTTPConnection object at 0x7fb41c6f4470>: Failed to establish a new connection: [Errno -5] No address associated with hostname

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "~/venv/lib64/python3.4/site-packages/requests/adapters.py", line 403, in send
    timeout=timeout
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 623, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/util/retry.py", line 281, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
requests.packages.urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='google.com', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7fb41c6f4470>: Failed to establish a new connection: [Errno -5] No address associated with hostname',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "~/venv/lib64/python3.4/site-packages/requests/api.py", line 71, in get
    return request('get', url, params=params, **kwargs)
  File "~/venv/lib64/python3.4/site-packages/requests/api.py", line 57, in request
    return session.request(method=method, url=url, **kwargs)
  File "~/venv/lib64/python3.4/site-packages/requests/sessions.py", line 475, in request
    resp = self.send(prep, **send_kwargs)
  File "~/venv/lib64/python3.4/site-packages/requests/sessions.py", line 585, in send
    r = adapter.send(request, **kwargs)
  File "~/venv/lib64/python3.4/site-packages/requests/adapters.py", line 467, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='google.com', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7fb41c6f4470>: Failed to establish a new connection: [Errno -5] No address associated with hostname',))
>>> 
```

I expect the second get() to work. Particularly if I've used a Session explicitly.

Wifi OFF

```
$ python
Python 3.4.3 (default, Mar 31 2016, 20:42:37) 
[GCC 5.3.1 20151207 (Red Hat 5.3.1-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> sess = requests.session()
>>> sess.get('http://www.google.com')
Traceback (most recent call last):
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/connection.py", line 142, in _new_conn
    (self.host, self.port), self.timeout, **extra_kw)
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/util/connection.py", line 67, in create_connection
    for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
  File "/usr/lib64/python3.4/socket.py", line 533, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -5] No address associated with hostname

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 578, in urlopen
    chunked=chunked)
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 362, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib64/python3.4/http/client.py", line 1088, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib64/python3.4/http/client.py", line 1126, in _send_request
    self.endheaders(body)
  File "/usr/lib64/python3.4/http/client.py", line 1084, in endheaders
    self._send_output(message_body)
  File "/usr/lib64/python3.4/http/client.py", line 922, in _send_output
    self.send(msg)
  File "/usr/lib64/python3.4/http/client.py", line 857, in send
    self.connect()
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/connection.py", line 167, in connect
    conn = self._new_conn()
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/connection.py", line 151, in _new_conn
    self, "Failed to establish a new connection: %s" % e)
requests.packages.urllib3.exceptions.NewConnectionError: <requests.packages.urllib3.connection.HTTPConnection object at 0x7f8e83e0ab00>: Failed to establish a new connection: [Errno -5] No address associated with hostname

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "~/venv/lib64/python3.4/site-packages/requests/adapters.py", line 403, in send
    timeout=timeout
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 623, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/util/retry.py", line 281, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
requests.packages.urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='www.google.com', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7f8e83e0ab00>: Failed to establish a new connection: [Errno -5] No address associated with hostname',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "~/venv/lib64/python3.4/site-packages/requests/sessions.py", line 487, in get
    return self.request('GET', url, **kwargs)
  File "~/venv/lib64/python3.4/site-packages/requests/sessions.py", line 475, in request
    resp = self.send(prep, **send_kwargs)
  File "~/venv/lib64/python3.4/site-packages/requests/sessions.py", line 585, in send
    r = adapter.send(request, **kwargs)
  File "~/venv/lib64/python3.4/site-packages/requests/adapters.py", line 467, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='www.google.com', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7f8e83e0ab00>: Failed to establish a new connection: [Errno -5] No address associated with hostname',))
>>> sess.close()
>>> del sess

```

wifi ON, google.com reachable from browser

```
>>> sess = requests.session()
>>> sess.get('http://www.google.com')
Traceback (most recent call last):
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/connection.py", line 142, in _new_conn
    (self.host, self.port), self.timeout, **extra_kw)
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/util/connection.py", line 67, in create_connection
    for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
  File "/usr/lib64/python3.4/socket.py", line 533, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -5] No address associated with hostname

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 578, in urlopen
    chunked=chunked)
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 362, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib64/python3.4/http/client.py", line 1088, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib64/python3.4/http/client.py", line 1126, in _send_request
    self.endheaders(body)
  File "/usr/lib64/python3.4/http/client.py", line 1084, in endheaders
    self._send_output(message_body)
  File "/usr/lib64/python3.4/http/client.py", line 922, in _send_output
    self.send(msg)
  File "/usr/lib64/python3.4/http/client.py", line 857, in send
    self.connect()
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/connection.py", line 167, in connect
    conn = self._new_conn()
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/connection.py", line 151, in _new_conn
    self, "Failed to establish a new connection: %s" % e)
requests.packages.urllib3.exceptions.NewConnectionError: <requests.packages.urllib3.connection.HTTPConnection object at 0x7f8e83e234e0>: Failed to establish a new connection: [Errno -5] No address associated with hostname

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "~/venv/lib64/python3.4/site-packages/requests/adapters.py", line 403, in send
    timeout=timeout
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 623, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "~/venv/lib64/python3.4/site-packages/requests/packages/urllib3/util/retry.py", line 281, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
requests.packages.urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='www.google.com', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7f8e83e234e0>: Failed to establish a new connection: [Errno -5] No address associated with hostname',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "~/venv/lib64/python3.4/site-packages/requests/sessions.py", line 487, in get
    return self.request('GET', url, **kwargs)
  File "~/venv/lib64/python3.4/site-packages/requests/sessions.py", line 475, in request
    resp = self.send(prep, **send_kwargs)
  File "~/venv/lib64/python3.4/site-packages/requests/sessions.py", line 585, in send
    r = adapter.send(request, **kwargs)
  File "~/venv/lib64/python3.4/site-packages/requests/adapters.py", line 467, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='www.google.com', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7f8e83e234e0>: Failed to establish a new connection: [Errno -5] No address associated with hostname',))
>>> 

```


Hrm. This is what I'd call "somewhat puzzling". I'm travelling at the moment so I don't have time to dive into this right this second, but I'll aim to take a look in the next couple of days and see if I can repro this.


Ok, so here's the thing.

Requests has no implicit global state. That means that two subsequent `requests.get` calls use totally brand new state. The fact that you're seeing this problem suggests that the state at either the OS or Python level are causing problems.

I also can't reproduce this on OS X, which further suggests that this is a problem with your networking stack in some form.


So the fact that these are all `getaddrinfo` failures makes me think that this might be some sort of DNS caching. I don't think Python 3.4 does that, so I'm guessing Fedora is doing it. I also don't think there's a way that Requests can work around this. And I don't know Fedora very well but @ralphbean does. Maybe he can help us out here?


Debian 8
Python 3.4.2 and 2.7.9
Requests 2.4.3
Couldn't reproduce the behavior.


@AlexPHorta I appreciate that but @shreepads is using 2.10.0 on Fedora 23


Closing for inactivity.