URLFetch 不支持精细的超时设置,恢复为总的或默认的 URLFetch 超时

URLFetch does not support granular timeout settings, reverting to total or default URLFetch timeout

我们有一个应用程序 (app-a) 在 python2.7 标准 google 应用引擎上。我们正在尝试使用基于示例 here 的服务帐户进行编程身份验证以访问另一个应用程序 (app-b)。 App-b 在 python3.7 标准 google 应用程序引擎上。

当我们执行 iap 身份验证调用时,我们在 App-A 的日志中收到以下错误。

resp = requests.request(
    method, url,
    headers={'Authorization': 'Bearer {}'.format(
        google_open_id_connect_token)}, **kwargs)

AppEnginePlatformWarning: urllib3 is using URLFetch on Google App Engine sandbox instead of sockets. To use sockets directly instead of URLFetch see https://urllib3.readthedocs.io/en/latest/reference/urllib3.contrib.html.

AppEnginePlatformWarning: URLFetch does not support granular timeout settings, reverting to total or default URLFetch timeout.

requests.request 错误

 Exception("Bad response from application: 500 / {'content-type': 'text/html', 'x-cloud-trace-context':

在 App-B 中,我们正在尝试接收从 app-a 发送的数据。

json.loads(request.data)

我们在 app-b 的日志中收到以下错误。

in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/opt/python3.7/lib/python3.7/json/decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

这让我相信 app-a 能够成功调用 app-b。但由于某种原因,它无法传递数据。请帮忙

更新 - 根据使用 HTTPlib 的建议,我们尝试了这个。

payload = {
    'test_data' : "data_test"
}

payload_json_dumps = json.dumps(payload)


conn = httplib.HTTPConnection(host)
conn.request("POST", path, payload_json_dumps, request_headers)
resp = conn.getresponse()

将以下内容添加到 app.yaml。

env_variables:
  GAE_USE_SOCKETS_HTTPLIB : 'true'

在App-B中我们改为

from flask import request

@app.route('url', methods = ['GET','POST'])
def content_from_client(): 
    if (request.data):
        data_received = request.get_json()

我们仍然无法获取 App-B 上的数据。我们得到

AttributeError("'NoneType' object has no attribute 'get'")

更新 -

更改了 header 编队并使其正常工作。

   request_headers = {
        "Content-Type": 'application/json', 
        "follow_redirects": False,
        "X-Appengine-Inbound-Appid": "app-A.appspot.com",
        "Connection": "keep-alive",
        'Authorization': 'Bearer {}'.format(google_open_id_connect_token)
    }

AppEnginePlatformWarningrequests library.

使用的 urllib3 引发 默认为

Urllib3 provides a pool manager that uses URL Fetch API。有时虽然可能不是您用例的最佳选择。一种解决方案是改用套接字。为此,您必须配置 app.yaml 并包含以下字段:

env_variables:
    GAE_USE_SOCKETS_HTTPLIB : 'true'

您还可以在 Google documentation 中找到此文档。

由于您的应用程序 B 出现错误,我将使用 response.json() 方法,requests 内置方法而不是 json.loads(),因为它会自动检测要使用的解码器。

让我知道这是否有帮助。

根据以上建议,以下内容有助于解决问题。更改为 httplib 而不是 urlfetch。

conn = httplib.HTTPConnection(host)
conn.request("POST", path, payload_json_dumps, request_headers)
resp = conn.getresponse()

将以下内容添加到 app.yaml -

 env_variables:
    GAE_USE_SOCKETS_HTTPLIB : 'true'

将 header 队形更改为 -

  request_headers = {
        "Content-Type": 'application/json', 
        "follow_redirects": False,
        "X-Appengine-Inbound-Appid": "app-A.appspot.com",
        "Connection": "keep-alive",
        'Authorization': 'Bearer {}'.format(google_open_id_connect_token)
    }