holinhot
V2EX  ›  问与答

python3 urllib 设置 Host header 无效问题

  •  
  •   holinhot · Sep 19, 2021 · 2067 views
    This topic created in 1871 days ago, the information mentioned may be changed or developed.

    import urllib.request

    headers = {  
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36",  
      "Host": ""  
    }  
    debug = True  
      
    import traceback  
    def g_post2(url, data, domain):  
        try:  
            headers['Host'] = domain  
            opener = urllib.request.build_opener()  
            opener.addheaders = [  
                ('User-Agent', headers['User-Agent']),  
      ('Host', domain)  
            ]  
      
            print(opener.addheaders)  
      
            return opener.open(url, data, timeout=2).read().decode()  
        except Exception as e:  
            print(e)  
            if debug:  
                traceback.print_exc()  
      
    print(g_post2('http://httpbin.org/post','data'.encode(), 'qq.com'))
    

    输出的结果看设置的 UA 是生效了,但设置的 Host 没有生效。是不是设置的 Host 被后面的 open 覆盖掉了,但是 open 又没提供 headers 设置参数。

    [('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36'), ('Host', 'qq.com')]
    {
      "args": {}, 
      "data": "", 
      "files": {}, 
      "form": {
        "data": ""
      }, 
      "headers": {
        "Accept-Encoding": "identity", 
        "Content-Length": "4", 
        "Content-Type": "application/x-www-form-urlencoded", 
        "Host": "httpbin.org", 
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36", 
        "X-Amzn-Trace-Id": "Root=1-6146527f-471edcb1073f7ebe3e0672a3"
      }, 
      "json": null, 
      "origin": "123.203.104.151", 
      "url": "http://httpbin.org/post"
    }
    
    8 replies  •  2021-09-19 18:20:32 +08:00
    Trim21
    Trim21
    Sep 19, 2021❤️1
    1
    设置的 header 在 open 里面被覆盖掉了。

    直接用 urlopen 和 Request 不就好了

    with urllib.request.urlopen(urllib.request.Request(url, data, method='GET', headers=headers)) as res:
    ....return res.read().decode()
    simple2025
    simple2025
    Sep 19, 2021
    2
    有个问题,能用 requests 吗?
    locoz
    locoz
    Sep 19, 2021
    3
    所以都这年头了为啥还在直接用 urllib...
    ch2
    ch2
    Sep 19, 2021
    4
    Host 是强制取 url 里的,你不能覆盖这个请求头
    MiketsuSmasher
    MiketsuSmasher
    Sep 19, 2021
    5
    直接用 requests 不是更爽吗,你们的项目是什么性质的,居然要用 urllib
    holinhot
    holinhot
    OP
    Sep 19, 2021
    6
    @chenqh 性能上 requests 差点
    holinhot
    holinhot
    OP
    Sep 19, 2021
    7
    @Trim21 这算 BUG 吧
    holinhot
    holinhot
    OP
    Sep 19, 2021
    8
    @Trim21 加了代理。request.Request(好像不行啊
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1734 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 17:06 · PVG 01:06 · LAX 09:06 · JFK 12:06
    ♥ Do have faith in what you're doing.