【问题标题】:How can I suppress the urlfetch headers warning in Google App Engine?如何抑制 Google App Engine 中的 urlfetch 标头警告?
【发布时间】:2012-03-16 15:41:54
【问题描述】:

每当我在 GAE 上使用 urlfetch 请求外部 URL 时,我都会收到以下警告:

WARNING  2012-03-16 15:37:21,474 urlfetch_stub.py:428] Stripped prohibited headers from URLFetch request: ['Content-Length']

我了解为什么会发生这种情况,并且我无法阻止根本问题。有没有办法可以抑制这个警告,以免它阻塞日志?当然,我仍然想知道 urlfetch 想要记录的任何其他警告/错误。

【问题讨论】:

    标签: python google-app-engine urlfetch


    【解决方案1】:

    无法从日志中抑制它,您必须抑制 Content-type 标头。

    【讨论】:

    • 当我不控制它发出请求的服务器时,这可能吗?这是我正在调用的 Google API。
    • 除非我弄错了,否则 urlfetch 会抱怨您在请求中设置的标头,因此服务器的行为不会进入其中。 (而且我不明白为什么 Content-Length 会在响应中被禁止——这不会使几乎所有响应都无效吗?)
    • 啊,好的。这更有意义。谢谢!
    • 当您说“您必须取消 Content-type 标头”时,您的回答是什么意思?你是说如果你压制它 - 那么这不会出现在日志中?还是你说不可能?如果可以,怎么做?
    【解决方案2】:

    这个警告很烦人。

    这是一个补丁。它也适用于 urllib2、urllib3 和 Requests。

    from google.appengine.api import urlfetch
    
    urlfetch.fetch_body = urlfetch.fetch
    
    def fetch_patch(url, payload=None, method=1, headers={},
                    allow_truncated=False, follow_redirects=True,
                    deadline=None, validate_certificate=None):
        if headers and headers.get('Content-Length', None):
            del headers['Content-Length']
        if headers and headers.get('Host', None):
            del headers['Host']
    
        return urlfetch.fetch_body(url, payload, method, headers,
                                   allow_truncated, follow_redirects,
                                   deadline, validate_certificate)
    
    urlfetch.fetch = fetch_patch
    

    【讨论】:

      猜你喜欢
      • 2011-02-02
      • 1970-01-01
      • 2011-01-10
      • 2010-12-24
      • 1970-01-01
      • 1970-01-01
      • 2015-04-10
      • 2012-08-14
      相关资源
      最近更新 更多