【发布时间】:2012-03-25 04:14:43
【问题描述】:
我可以在 Google App Engine 上使用 Requests 吗?我认为这个库非常适合创建 REST 客户端。
【问题讨论】:
标签: python google-app-engine python-requests
我可以在 Google App Engine 上使用 Requests 吗?我认为这个库非常适合创建 REST 客户端。
【问题讨论】:
标签: python google-app-engine python-requests
不,在more recent post 中,开发人员说他们不支持 GAE,因为它与 python 太不同了。
是的。在 Google Appengine(版本 1.9.18)requests版本 2.3.0 上工作在生产中(但不是在 SDK 上)如果您启用了计费,这启用了套接字支持。
更新:截至 2017 年 1 月 31 日,Requests 正在生产中,版本 2.9.1 正在生产中。但是,它不适用于 Google Cloud SDK 141.0.0
Appengine SDK 上的请求因所有 https:// 请求而失败:
ConnectionError: ('Connection aborted.', error(13, 'Permission denied'))
请求版本 2.4.1 在生产中失败:
File "distlib/requests/adapters.py", line 407, in send
raise ConnectionError(err, request=request)
ConnectionError: ('Connection aborted.', error(13, 'Permission denied'))
请求版本 2.5.1 在生产中失败:
File "distlib/requests/adapters.py", line 415, in send
raise ConnectionError(err, request=request)
ConnectionError: ('Connection aborted.', error(13, 'Permission denied'))
requests 版本 2.3.0 可以在生产中使用,但可能会导致 Debians 在 SDK 中删除 SSLv3 支持(requests 2.3.0 带有自己的现已过时的 urllib3)。作为解决方法,可以删除请求的 urllib3 副本源中包含 PROTOCOL_SSLv3 的行。
'module' object has no attribute 'PROTOCOL_SSLv3'
关于套接字支持的信息:https://cloud.google.com/appengine/docs/python/sockets/
【讨论】:
ssl 库为我解决了这个问题!谢谢
安装requests-toolbelt 库:
https://github.com/sigmavirus24/requests-toolbelt
对于 App Engine,它可能类似于:pip install requests-toolbelt -t lib
(参见:https://cloud.google.com/appengine/docs/python/tools/using-libraries-python-27#installing_a_library)
然后添加:
from requests_toolbelt.adapters import appengine
appengine.monkeypatch()
在您的main.py 或同等学历中。
编辑:这个解决方案现在是官方文档的一部分:https://cloud.google.com/appengine/docs/python/issue-requests#issuing_an_http_request
(在REQUESTS 标签中)
【讨论】:
urlfetch,它在免费配额下可用,但有一些限制。例如,它目前不支持客户端证书,它适用于使用套接字的approach in the currently accepted answer。
这现在是可能的,我在 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'
【讨论】:
def platform()不需要缩进
是的,您可以使用请求模块。 GCP 不支持开箱即用的 Requests 库。因此,我们将不得不花费一些时间来使其发挥作用。 为了在 Google App Engine 上部署应用程序,我们需要创建一个 main.py(主要 python 烧瓶应用程序所在的位置)和 app.yaml(在 GCP 中运行它所需的配置文件)。 这是python 2.7环境的app.yaml文件的示例代码
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
redirect_http_response_code: 301
script: main.app
libraries:
- name: flask
version: 0.12
现在我们需要配置请求库以在内部使用 URLfetch。 要使用 requests,我们需要使用 vendoring 说明安装 requests 和 requests-toolbelt。 (https://cloud.google.com/appengine/docs/standard/python/tools/using-libraries-python-27#installing_a_library)。 基本上我们需要安装我们的自定义库。
编辑 appengine_config.py 文件并将您的库目录提供给 vendor.add() 方法。 示例 appengine_config.py 文件
from google.appengine.ext import vendor
# Add any libraries install in the "lib" folder.
vendor.add('lib/requests')
vendor.add('lib/requests_toolbelt')
安装后,使用 requests_toolbelt.adapters.appengine 模块将请求配置为使用 URLFetch。 将以下代码复制到 main.py 文件的开头
import requests
from requests_toolbelt.adapters import appengine
appengine.monkeypatch(validate_certificate=True)
(https://cloud.google.com/appengine/docs/standard/python/issue-requests)
现在我们可以轻松地使用 requests 库来发出 get/post 请求。 测试您的应用:
dev_appserver.py --port=<port number> app.yaml
【讨论】:
为了修复使用 Google App Engine 的请求,同时允许我的应用程序在 GAE 之外运行,我添加了以下代码:
try:
from google.appengine.api import urlfetch
from requests_toolbelt.adapters import appengine
appengine.monkeypatch()
except ImportError:
pass
【讨论】: