Can I use Requests on Google App Engine? I think this library is perfect to create a REST client.
Install the requests-toolbelt
library: https://github.com/sigmavirus24/requests-toolbelt
For App Engine it could be something like: pip install requests-toolbelt -t lib
(See: https://cloud.google.com/appengine/docs/python/tools/using-libraries-python-27#installing_a_library )
Then add:
from requests_toolbelt.adapters import appengine
appengine.monkeypatch()
In your main.py
or equivalent.
Edit : this solution is now part of the official documentation: https://cloud.google.com/appengine/docs/python/issue-requests#issuing_an_http_request (in the REQUESTS
tab)
Yes. On Google Appengine (version 1.9.18) requests version 2.3.0 works IN PRODUCTION (but not on SDK) if you have billing enabled, which enables sockets support.
Update : As of 31 Jan 2017 Requests is working in production with version 2.9.1 in production. However, it's not working with Google cloud SDK 141.0.0
requests on the Appengine SDK fails with all https:// requests:
ConnectionError: ('Connection aborted.', error(13, 'Permission denied'))
requests version 2.4.1 fails in production with:
File "distlib/requests/adapters.py", line 407, in send
raise ConnectionError(err, request=request)
ConnectionError: ('Connection aborted.', error(13, 'Permission denied'))
requests version 2.5.1 fails in production with:
File "distlib/requests/adapters.py", line 415, in send
raise ConnectionError(err, request=request)
ConnectionError: ('Connection aborted.', error(13, 'Permission denied'))
requests version 2.3.0 works in production, but may cause problems with Debians removed SSLv3 support in the SDK (requests 2.3.0 comes with its own now outdated urllib3). As workaround it is possible to delete the line containing PROTOCOL_SSLv3 in the source of request's urllib3 copy.
'module' object has no attribute 'PROTOCOL_SSLv3'
Info on sockets support: https://cloud.google.com/appengine/docs/python/sockets/
Not yet but hopefully very soon. Support for GAE is being worked on - see issue #498 (App Engine Fixes).
Requests uses urllib3 which in turn uses httplib which is supported on GAE as a wrapper for urlfetch API . Although urllib3 uses some modules not available on GAE this usage is deliberately made optional so that urllib3 can be used on GAE.
this is now possible, I got it working using this combination of workarounds in appengine_config.py:
# Step 1: first add requests and requests-toolbelt to your requirements.txt (or however you install them via pip)
# Step 2: in appengine_config.py add the following snippet:
# see https://cloud.google.com/appengine/docs/python/issue-requests#issuing_an_http_request
import requests
import requests_toolbelt.adapters.appengine
# Use the App Engine Requests adapter. This makes sure that Requests uses
# URLFetch.
requests_toolbelt.adapters.appengine.monkeypatch()
# also monkey patch platform.platform() from https://code.google.com/p/googleappengine/issues/detail?id=12982
import platform
def patch(module):
def decorate(func):
setattr(module, func.func_name, func)
return func
return decorate
@patch(platform)
def platform():
return 'AppEngine'
Yes you can use the requests module. GCP does not support the use of Requests library out of the box. So we will have to make some tweeks to make it work. In order to deploy an application on the Google App Engine, we need to make a main.py(where main python flask app resides) and app.yaml(configuration file needed to run it in GCP). Here is a sample code for the app.yaml file for python 2.7 environment
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
redirect_http_response_code: 301
script: main.app
libraries:
- name: flask
version: 0.12
Now we need to configure the requests library to use URLfetch internally. To use requests, we'll need to install both requests and requests-toolbelt using the vendoring instructions. ( https://cloud.google.com/appengine/docs/standard/python/tools/using-libraries-python-27#installing_a_library ). Basically we need to install our custom libraries.
Edit the appengine_config.py file and provide your library directory to the vendor.add() method. Sample appengine_config.py file
from google.appengine.ext import vendor # Add any libraries install in the "lib" folder. vendor.add('lib/requests') vendor.add('lib/requests_toolbelt')
Once installed, use the requests_toolbelt.adapters.appengine module to configure requests to use URLFetch. Copy the below code to the start of your main.py file
import requests from requests_toolbelt.adapters import appengine appengine.monkeypatch(validate_certificate=True)
( https://cloud.google.com/appengine/docs/standard/python/issue-requests )
Now we can easily use the requests library to make get/post requests. Test your app:
dev_appserver.py --port=<port number> app.yaml
To fix up requests to work with Google App Engine but also allow my application to run outside of GAE, I added the following code:
try:
from google.appengine.api import urlfetch
from requests_toolbelt.adapters import appengine
appengine.monkeypatch()
except ImportError:
pass
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.