33

我可以在 Google App Engine 上使用请求吗?我认为这个库非常适合创建 REST 客户端。

4

7 回答 7

42

安装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标签中)

于 2016-05-18T15:52:07.843 回答
27

是的。在 Google Appengine(版本 1.9.18)请求 版本 2.3.0工作在生产中(但不是在 SDK 上)如果您启用了计费,这启用了套接字支持。

更新:截至 2017 年 1 月 31 日,Requests 正在生产中,版本 2.9.1 正在生产中。但是,它不适用于 Google 云 SDK 141.0.0

Appengine SDK 上的请求因所有 https:// 请求而失败:

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

requests 2.4.1 版在生产中失败,原因如下:

  File "distlib/requests/adapters.py", line 407, in send
    raise ConnectionError(err, request=request)
  ConnectionError: ('Connection aborted.', error(13, 'Permission denied'))

requests 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/

于 2015-02-16T15:29:46.863 回答
8

还没有,但希望很快。正在努力支持 GAE - 请参阅问题#498(App Engine 修复)。

Requests 使用urllib3 ,后者又使用GAE 支持httplib作为urlfetch API的包装器。尽管 urllib3 使用了一些 GAE 上不可用的模块,但故意将此用法设为可选,以便 urllib3 可以在 GAE 上使用。

于 2012-04-07T18:13:06.330 回答
8

现在这是可能的,我在 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'
于 2016-09-19T20:21:27.303 回答
3

不,在最近的一篇文章中,开发人员说他们不支持 GAE,因为它与 python 太不同了。

于 2014-07-23T16:34:12.993 回答
2

为了修复使用 Google App Engine 的请求,但也允许我的应用程序在 GAE 之外运行,我添加了以下代码:

try:
    from google.appengine.api import urlfetch
    from requests_toolbelt.adapters import appengine
    appengine.monkeypatch()
except ImportError:
    pass
于 2019-01-06T01:03:37.043 回答
0

是的,您可以使用请求模块。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

现在我们需要配置 requests 库以在内部使用 URLfetch。要使用 requests,我们需要使用 vendoring 说明安装 requests 和 requests-toolbelt。(https://cloud.google.com/appengine/docs/standard/python/tools/using-libraries-python-27#installing_a_library)。基本上我们需要安装我们的自定义库。

  1. 创建一个目录来存放你的第三方库,例如 lib/ mkdir lib
  2. 从您的系统上传 requests 和 requests-toolbelt 库,或将它们直接下载到前面步骤中创建的 lib 文件夹中。
  3. 使用带有 -t 标志的 pip(版本 6 或更高版本)将库复制到您在上一步中创建的文件夹中。例如:pip install -t lib/ (pip install -t lib/ 请求)
  4. 在 app.yaml 文件所在的文件夹中创建一个名为 appengine_config.py 的文件。
  5. 编辑 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')
    
  6. 安装后,使用 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
于 2018-09-17T08:02:00.630 回答