Search code examples
pythongoogle-app-enginesslbraintreedev-appserver

Workaround for dev_appserver.py inability to make SSL requests


Ran into this problem trying to call braintree.ClientToken.generate() from a Google App Engine app, running Flask on dev_appserver.py. dev_appserver.py can't currently make outgoing SSL connections. Making the above braintree call yields

ConnectionError: ('Connection aborted.', error(13, 'Permission denied'))

The call works on in a real GAE environment. It's used in one of my views, so it breaks my whole website flow with the above 500 error when it fails. How can I work around this so that I can continue developing in my local environment?


Solution

  • I work at Braintree. If you have more questions, you can always contact our support team

    For help with the Braintree Python library on GAE, see this example on my GitHub. To answer your question, you can force the dev server to use the real Python socket library, so SSL connections work:

    try:
        # This is needed to make local development work with SSL.
        # This must be done *before* you import the Braintree Python library.
        # See http://stackoverflow.com/a/24066819/500584
        # and https://code.google.com/p/googleappengine/issues/detail?id=9246 for more information.
        from google.appengine.tools.devappserver2.python import sandbox
        sandbox._WHITE_LIST_C_MODULES += ['_ssl', '_socket']
    
        import sys
        # this is socket.py copied from a standard python install
        import stdlib_socket
        sys.modules['socket'] = stdlib_socket
    except ImportError as e:
        print(e)