ConnectionError with App Engine

Hi,

I'm using App Engine & Requests library. So far, I haven't had any problems. I recently came across with something I couldn't solve:

I'm writing a web application that uses Facebook Graph API. So far, Requests have worked well but I'm now trying to add a somewhat costly query, and I bump into an error.

The following does NOT work (I use MYFACEBOOKID and MYTOKEN, please change it to relevant values please):

``` python
import fix_path
import webapp2
from lib import requests

class MainHandler(webapp2.RequestHandler):
    def get(self):
        url = 'https://graph.facebook.com/MYFACEBOOKID?access_token=MYTOKEN&fields=music.fields(id,name,hometown),gender,birthday,name,friends.fields(id,gender,name,birthday,music.fields(id,name,hometown))&method=GET&format=json'
        r = requests.get(url)
        self.response.write(r.json)

app = webapp2.WSGIApplication([
    ('/', MainHandler)
], debug=True)
```

For the above code piece, I get the following error:

``` python
Traceback (most recent call last):
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1536, in __call__
    rv = self.handle_exception(request, response, e)
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1530, in __call__
    rv = self.router.dispatch(request, response)
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/base/data/home/apps/s~benzeshen1/oguz.363884243820635627/main.py", line 30, in get
    r = requests.get(url)
  File "/base/data/home/apps/s~benzeshen1/oguz.363884243820635627/lib/requests/api.py", line 63, in get
    return request('get', url, **kwargs)
  File "/base/data/home/apps/s~benzeshen1/oguz.363884243820635627/lib/requests/api.py", line 49, in request
    return session.request(method=method, url=url, **kwargs)
  File "/base/data/home/apps/s~benzeshen1/oguz.363884243820635627/lib/requests/sessions.py", line 269, in request
    return self._send_request(r, **args)
  File "/base/data/home/apps/s~benzeshen1/oguz.363884243820635627/lib/requests/sessions.py", line 273, in _send_request
    r.send(prefetch=kwargs.get("prefetch"))
  File "/base/data/home/apps/s~benzeshen1/oguz.363884243820635627/lib/requests/models.py", line 641, in send
    raise ConnectionError(e)
ConnectionError: HTTPSConnectionPool(host='graph.facebook.com', port=443): Max retries exceeded with url: /MYFACEBOOKID?access_token=MYTOKEN&fields=music.fields(id,name,hometown),gender,birthday,name,friends.fields(id,gender,name,birthday,music.fields(id,name,hometown))&method=GET&format=json
```

However, this works using App Engine urlfetch:

``` python
import fix_path
import webapp2
from google.appengine.api import urlfetch

class MainHandler(webapp2.RequestHandler):
    def get(self):
        url = 'https://graph.facebook.com/MYFACEBOOKID?access_token=MYTOKEN&fields=music.fields(id,name,hometown),gender,birthday,name,friends.fields(id,gender,name,birthday,music.fields(id,name,hometown))&method=GET&format=json'
        result = urlfetch.fetch(url, deadline=60)
        if result.status_code == 200:
            self.response.write(result.content)

app = webapp2.WSGIApplication([
    ('/', MainHandler)
], debug=True)
```

How can I solve this issue?


App engine is not supported, as it is not Python.


in using Requests on App Engine, and hitting the Facebook Graph API, I was debugging a similar issue with:

```
ConnectionError: HTTPSConnectionPool(host='graph.facebook.com', port=443): Max retries exceeded
```

occurring.  I believe this is just an unfortunate error in the SSL layer that is not the actual error occurring.

By switching to using the `data=` arg in the `requests.post` and also adding the `verify=False` arg to prevent sensitivity around SSL, I was able to get the call through without the `ConnectionError` being raised.

See:
http://docs.python-requests.org/en/latest/api/#lower-level-classes

In my case, the response I got from Facebook was:

```
{u'error': {u'message': u'Too many requests in batch message. Maximum batch size is 50',
  u'type': u'GraphBatchException'}}
```

Thus Facebook is just not liking the number of things in my batch call, but the wrong error is being raised and confusing things immensely.


I broke it while enabling SNI in urllib3.
It will be fixed by shazow/urllib3#130 (ping @shazow)
Try removing the `ssl_wrap_socket()` function from `requests.packages.urllib3.util.py`


Ping received. I plan to put a few hours into urllib3 this weekend. I'll put this one at the top of my list.


(Master has it now)


Per @sigmavirus24, I'm watching this as a possible root cause of a similar-looking problem in Gittip, hitting Twitter: https://github.com/zetaweb/www.gittip.com/issues/531.