代码之家  ›  专栏  ›  技术社区  ›  Nishant Florentin

在Python中建立HTTPS连接时,是否应该在头中传递host?

  •  0
  • Nishant Florentin  · 技术社区  · 4 年前

    我得到一个 HTTP 400 - Bad Request 在进行 XML-RPC HTTPS 在Python3.8中。

    Host 中的标题 请求,没有 skip_host=True putrequest doc )在那之前打电话。这两个信息都是-- skip_host 主持人

    import http.client
    
    connection = http.client.HTTPSConnection("duckduckgo.com", "443")
    
    connection.putrequest("GET", "/")  # needs skip_host=True if Host has to be supplied
    connection.putheader("User-Agent", "Python/3.8")
    connection.putheader("Host", "duckduckgo.com")  # needs skip_host=True to work
    connection.endheaders()
    
    response = connection.getresponse()
    print(response.status, response.reason)
    

    0 回复  |  直到 4 年前
        1
  •  4
  •   Nikolaos Chatzis    4 年前

    离开 skip_host 默认值,即。, False Host putheader 导致发送 主持人 头两次(在本例中使用不同的值)。这可以通过设置 debuglevel

    >>> import http.client
    >>> connection = http.client.HTTPSConnection("duckduckgo.com", "443")
    >>> connection.set_debuglevel(1)
    >>> connection.putrequest("GET", "/")
    >>> connection.putheader("User-Agent", "Python/3.8")
    >>> connection.putheader("Host", "duckduckgo.com")
    >>> connection.endheaders()
    send: b'GET / HTTP/1.1\r\nHost: duckduckgo.com:443\r\nAccept-Encoding: identity\r\nUser-Agent: Python/3.8\r\nHost: duckduckgo.com\r\n\r\n'
    >>> 
    >>> response = connection.getresponse()
    reply: 'HTTP/1.1 400 Bad Request\r\n'
    header: Server header: Date header: Content-Type header: Content-Length header: Connection header: X-XSS-Protection header: X-Content-Type-Options header: Referrer-Policy header: Expect-CT
    

    http.client 's code 有人提到 主持人 putrequest :

                if not skip_host:
                    # this header is issued *only* for HTTP/1.1
                    # connections. more specifically, this means it is
                    # only issued when the client uses the new
                    # HTTPConnection() class. backwards-compat clients
                    # will be using HTTP/1.0 and those clients may be
                    # issuing this header themselves. we should NOT issue
                    # it twice; some web servers (such as Apache) barf
                    # when they see two Host: headers
    

    您的代码可以通过添加 skip_host=True 或者不明确指定 标题。两者都会导致 主持人

    >>> import http.client
    >>> connection = http.client.HTTPSConnection("duckduckgo.com", "443")
    >>> connection.putrequest("GET", "/", skip_host=True)
    >>> connection.putheader("User-Agent", "Python/3.8")
    >>> connection.putheader("Host", "duckduckgo.com")
    >>> connection.endheaders()
    >>> response = connection.getresponse()
    >>> print(response.status, response.reason)
    200 OK
    >>> # OR
    >>> connection = http.client.HTTPSConnection("duckduckgo.com", "443")
    >>> connection.putrequest("GET", "/")
    >>> connection.putheader("User-Agent", "Python/3.8")
    >>> connection.endheaders()
    >>> response = connection.getresponse()
    >>> print(response.status, response.reason)
    200 OK
    

    至于要用哪一个 docs 似乎在暗示除非你有理由 主持人 标题(使用 putheader公司 )您可以依靠模块自动发送 主持人 跳过\u主机 默认值,即。, ,并且不指定 标题使用