第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

來自請求 Python 庫的 HTTP 請求中缺少主機標(biāo)頭

來自請求 Python 庫的 HTTP 請求中缺少主機標(biāo)頭

慕森卡 2022-05-19 14:20:21
Python 庫生成的 HTTP 請求消息中的HTTP/1.1 強制 Host 頭字段在哪里?requestsimport requestsresponse = requests.get("https://www.google.com/")print(response.request.headers)輸出:{'User-Agent': 'python-requests/2.22.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
查看完整描述

1 回答

?
MM們

TA貢獻(xiàn)1886條經(jīng)驗 獲得超2個贊

默認(rèn)情況下,HOST標(biāo)頭不會添加到請求中requests。如果沒有明確添加,則將決策委托給底層http模塊。


請參閱以下部分http/client.py:


(如果在then is'Host'中明確提供了標(biāo)題)requests.getskip_hostTrue


    if self._http_vsn == 11:

        # Issue some standard headers for better HTTP/1.1 compliance


        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


            # If we need a non-standard port,include it in the

            # header.  If the request is going through a proxy,

            # but the host of the actual URL, not the host of the

            # proxy.


            netloc = ''

            if url.startswith('http'):

                nil, netloc, nil, nil, nil = urlsplit(url)


            if netloc:

                try:

                    netloc_enc = netloc.encode("ascii")

                except UnicodeEncodeError:

                    netloc_enc = netloc.encode("idna")

                self.putheader('Host', netloc_enc)

            else:

                if self._tunnel_host:

                    host = self._tunnel_host

                    port = self._tunnel_port

                else:

                    host = self.host

                    port = self.port


                try:

                    host_enc = host.encode("ascii")

                except UnicodeEncodeError:

                    host_enc = host.encode("idna")


                # As per RFC 273, IPv6 address should be wrapped with []

                # when used as Host header


                if host.find(':') >= 0:

                    host_enc = b'[' + host_enc + b']'


                if port == self.default_port:

                    self.putheader('Host', host_enc)

                else:

                    host_enc = host_enc.decode("ascii")

                    self.putheader('Host', "%s:%s" % (host_enc, port)) 

因此,'Host'在檢查requests發(fā)送到服務(wù)器的標(biāo)頭時,我們看不到標(biāo)頭。


如果我們向http://httpbin/get發(fā)送請求并打印響應(yīng),我們可以看到Host標(biāo)頭確實已發(fā)送。


import requests


response = requests.get("http://httpbin.org/get")

print('Response from httpbin/get')

print(response.json())

print()

print('response.request.headers')

print(response.request.headers)

輸出


Response from httpbin/get

{'args': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 

 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.20.0'},

 'origin': 'XXXXXX', 'url': 'https://httpbin.org/get'}


response.request.headers

{'User-Agent': 'python-requests/2.20.0', 'Accept-Encoding': 'gzip, deflate', 

 'Accept': '*/*', 'Connection': 'keep-alive'}


查看完整回答
反對 回復(fù) 2022-05-19
  • 1 回答
  • 0 關(guān)注
  • 222 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號