AppEnginePlatformWarning:urllib3 在 Google App Engine 沙箱上使用 URLFetch 而不是套接字

Posted

技术标签:

【中文标题】AppEnginePlatformWarning:urllib3 在 Google App Engine 沙箱上使用 URLFetch 而不是套接字【英文标题】:AppEnginePlatformWarning: urllib3 is using URLFetch on Google App Engine sandbox instead of sockets 【发布时间】:2017-06-12 14:31:33 【问题描述】:

我在使用烧瓶制作 Slack 机器人的应用引擎上遇到此错误。每当我从 Slackbot 发送 POST 请求时都会发生这种情况。

很遗憾,错误中提供的 url 是一个死链接。如何使用套接字而不是 URLFetch?

/base/data/home/apps/[已编辑]/lib/requests/packages/urllib3/contrib/appengine.py:115: AppEnginePlatformWarning:urllib3 正在 Google 应用上使用 URLFetch 引擎沙箱而不是套接字。直接使用套接字而不是 URLFetch 见https://urllib3.readthedocs.io/en/latest/contrib.html

【问题讨论】:

正确的网址是:urllib3.readthedocs.io/en/latest/reference/urllib3.contrib.html 【参考方案1】:

如Google's Sockets documentation 中所述,可以通过设置GAE_USE_SOCKETS_HTTPLIB 环境变量来使用套接字。此功能似乎仅适用于付费应用,并且会影响计费。

尽管您发布的错误在 App Engine 中被记录为错误,this thread 建议(请参阅回复 #8)该错误实际上是作为警告,文本“AppEnginePlatformWarning”似乎无论如何都暗示了这一点。

source page 上 appengine.py 的注释块也很有指导意义。

您没有发布任何有关您的实施的信息,而是在 Google App Engine 标准版 using URLFetch via the AppEngineManager should be just fine 上发布的,尽管您会收到错误消息。

【讨论】:

【参考方案2】:

您可以使用以下方法使其静音:

import warnings
import urllib3.contrib.appengine

warnings.filterwarnings('ignore', r'urllib3 is using URLFetch', urllib3.contrib.appengine.AppEnginePlatformWarning)

【讨论】:

我可以把这个放在哪里?我正在使用一个从内部执行此操作的库。我需要编辑库的源代码吗? @divij 将它放在您的 main() 函数的开头附近,或者就在您第一次调用库之前。它将适用于程序中的所有内容,直到它退出。 好的,我试试看! 以防万一 - 您可以将其添加到 appengine_config.py 以忽略警告 import warnings warnings.filterwarnings('ignore', r'urllib3')【参考方案3】:

对我来说,我的项目中存在 requests_toolbelt 依赖项是问题所在:它以某种方式强制请求库使用 urllib3,这需要 URLFetch 存在,否则 raises an AppEnginePlatformError。正如the app engine docs 中所建议的那样,使用 requests_toolbelt 对请求进行猴子修补会强制前者使用 URLFetch,而 GAE 在 Python 3 运行时中没有 longer supported。

解决方案是从我的requirements.txt 文件中删除requests_toolbelt

【讨论】:

以上是关于AppEnginePlatformWarning:urllib3 在 Google App Engine 沙箱上使用 URLFetch 而不是套接字的主要内容,如果未能解决你的问题,请参考以下文章