requests + gevent dns error

I seem to be getting a strange error using requests and gevent with the below code snippet:
running this under gunicorn and hitting this with: ab -n 5 -c 5 http://localhost:8000/
gives this output:

```
2013-02-19 19:32:24 [40251] [INFO] Starting gunicorn 0.17.2
2013-02-19 19:32:24 [40251] [INFO] Listening at: http://127.0.0.1:8000 (40251)
2013-02-19 19:32:24 [40251] [INFO] Using worker: gevent
2013-02-19 19:32:24 [40256] [INFO] Booting worker with pid: 40256
HTTPConnectionPool(host='httpbin.org', port=80): Max retries exceeded with url: /delay/5 (Caused by <class 'gevent.dns.DNSError'>: [Errno 3] name does not exist)
HTTPConnectionPool(host='httpbin.org', port=80): Max retries exceeded with url: /delay/5 (Caused by <class 'gevent.dns.DNSError'>: [Errno 3] name does not exist)
HTTPConnectionPool(host='httpbin.org', port=80): Max retries exceeded with url: /delay/5 (Caused by <class 'gevent.dns.DNSError'>: [Errno 3] name does not exist)
HTTPConnectionPool(host='httpbin.org', port=80): Max retries exceeded with url: /delay/5 (Caused by <class 'gevent.dns.DNSError'>: [Errno 3] name does not exist)
<Response [200]>
```

``` python
from flask import Flask
import requests

app = Flask(__name__)

@app.route('/')
def hello():
        try:
                r = requests.get('http://httpbin.org/delay/5')
                print r
        except Exception as e:
                print e.message
        return 'done'
```

these are the versions I'm using (all from pip install):

```
$ pip freeze
Flask==0.9
Jinja2==2.6
Werkzeug==0.8.3
argparse==1.2.1
distribute==0.6.24
gevent==0.13.8
greenlet==0.4.0
gunicorn==0.17.2
requests==1.1.0
wsgiref==0.1.2

```


This looks like you're failing to resolve the domain name. Assuming you're running this on a box you have control over, can you try running `nslookup httpbin.org` from the command prompt?


```
Server:     127.0.0.1
Address:    127.0.0.1#53

Non-authoritative answer:
Name:   httpbin.org
Address: 23.21.109.151
```

The call doesn't always fail with the DNS error.
The more connections I try to open at the same time the more likely it will fail.


That's bizarre.

The failure seems to be to do with gevent's DNS stack, however, not Requests: we're simply wrapping the exception. To see if you can isolate it, try just calling `gevent.dns.resolve_ipv4('httpbin.org')` in each of your flask apps, to see if you can trigger the same exceptions.


it appears that calling your suggested line just before calling requests makes the problem go away somehow.
so this seem to work every call:

``` python
gevent.dns.resolve_ipv4('httpbin.org')
r = requests.get('http://httpbin.org/delay/5')
```


Uh...that was not what I expected to happen.

My best guess is that gevent caches the DNS lookup from the first call so that the second one definitely succeeds, but that doesn't really explain very much. Hmm. I'll have to think about this.


Not really explaining what you're seeing, but do you still get this error with newer gevents?  ie. 1.0rc2 (https://github.com/SiteSupport/gevent/downloads)


I didn't have the time to check this issue with a newer version of gevent on the same computer but I did run the same test on another computer and I didn't observe the same errors on the other computer.


Sounds to me like this is somehow gevent's fault. =D Anyone disagree?


Nope


With no other objections, then, I'm closing this issue. =)


Was your gevent grabed by `pip`?
@oargon 
This bug probably was result from the low version of the gevent.
You can refer this: http://jesiah.net/post/59711878434/gevent-problems-gevent-dns-dnserror


I think I'm having the same problem with this.   Going to try adding that resolve line to see if it makes it go away.