AppEnginePlatformWarning - 使用套接字的理由? [英] AppEnginePlatformWarning - reason to use sockets?
问题描述
在Google App Engine标准环境中,如果您使用urllib发出HTTPS请求,则会得到一个 AppEnginePlatformWarning
,表示您使用的是urlfetch而不是套接字。
我发现警告很烦人,所以我禁用了它。
#使用App Engine请求适配器。这可以确保请求使用
#URLFetch。
requests_toolbelt.adapters.appengine.monkeypatch()
#squelch警告
requests.packages.urllib3.disable_warnings(
requests.packages.urllib3.contrib.appengine。 AppEnginePlatformWarning
)
我的问题是 - 是否有充足的理由切换到套接字?特别是使用urlfetch有什么问题?
使用 urlfetch $ c $没有任何问题事实上,它是在GAE上发出出站HTTP(S)请求的推荐方法。从发出HTTP(S)请求(强调<$
App Engine使用网址抓取服务来发出呼出HTTP(S)
请求。
有关如何实现网址提取服务以及在网址提取请求中发送哪些
标头的详细信息,请参阅出站请求。
发出HTTP请求
要发出出站HTTP请求,请使用
urlfetch.fetch
方法。为了改进代码的可移植性,您还可以使用Python
标准库urllib
,urllib2
或httplib
发出HTTP
请求。当您在App Engine中使用这些库时,它们使用App Engine的URL提取服务执行
HTTP请求。 只要您将它配置为使用
URLFetch,您也可以使用
第三方请求
库。
套接字支持在GAE中颇具问题,它具有相当长的限制和限制列表,请参阅套接字Python API概述,特别是限制和限制部分。 警告您看到的不是来自GAE,而是来自您使用的第三方请求
库,这就是为什么我在上面的引用中突出显示注释的原因。恕我直言,在GAE上下文中简单地忽略/掩盖警告是安全的。
In the Google App Engine standard environment, if you use urllib to make HTTPS requests, you'll get an AppEnginePlatformWarning
which says you're using urlfetch instead of sockets.
I found the warning annoying, so I disabled it.
# Use the App Engine Requests adapter. This makes sure that Requests uses
# URLFetch.
requests_toolbelt.adapters.appengine.monkeypatch()
# squelch warning
requests.packages.urllib3.disable_warnings(
requests.packages.urllib3.contrib.appengine.AppEnginePlatformWarning
)
My question is - is there a good reason to switch to sockets? Specifically what is wrong with using urlfetch?
There's nothing wrong with using urlfetch
, in fact it is the recommended method for issuing outbound HTTP(S) requests on GAE. From Issuing HTTP(S) Requests (emphasis on requests
-related note mine):
App Engine uses the URL Fetch service to issue outbound HTTP(S) requests.
For details about how the URL Fetch service is implemented and which headers are sent in a URL Fetch request, see Outbound Requests.
Issuing an HTTP request
To issue an outbound HTTP request, use the
urlfetch.fetch
method. For improved code portability, you can also use the Python standard librariesurllib
,urllib2
, orhttplib
to issue HTTP requests. When you use these libraries in App Engine, they perform HTTP requests using App Engine's URL Fetch service. You can also use the third-partyrequests
library as long as you configure it to use URLFetch.
The sockets support is rather the problematic one in GAE, it comes with a fairly long list of limitations and restrictions, see Sockets Python API Overview, in particular the Limitations and restrictions section.
The warning you see is not from GAE, it's from the 3rd-party requests
library you use, which is why I highlighted the note in the above quote. IMHO it's safe to simply ignore/mask the warning in a GAE context.
这篇关于AppEnginePlatformWarning - 使用套接字的理由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!