V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
holinhot
V2EX  ›  问与答

python3 urllib 设置 Host header 无效问题

  •  
  •   holinhot · 2021-09-19 05:00:58 +08:00 · 975 次点击
    这是一个创建于 942 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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 条回复    2021-09-19 18:20:32 +08:00
    Trim21
        1
    Trim21  
       2021-09-19 07:03:49 +08:00   ❤️ 1
    设置的 header 在 open 里面被覆盖掉了。

    直接用 urlopen 和 Request 不就好了

    with urllib.request.urlopen(urllib.request.Request(url, data, method='GET', headers=headers)) as res:
    ....return res.read().decode()
    chenqh
        2
    chenqh  
       2021-09-19 08:46:47 +08:00
    有个问题,能用 requests 吗?
    locoz
        3
    locoz  
       2021-09-19 08:49:31 +08:00
    所以都这年头了为啥还在直接用 urllib...
    ch2
        4
    ch2  
       2021-09-19 10:42:41 +08:00
    Host 是强制取 url 里的,你不能覆盖这个请求头
    MiketsuSmasher
        5
    MiketsuSmasher  
       2021-09-19 17:38:55 +08:00
    直接用 requests 不是更爽吗,你们的项目是什么性质的,居然要用 urllib
    holinhot
        6
    holinhot  
    OP
       2021-09-19 17:48:14 +08:00
    @chenqh 性能上 requests 差点
    holinhot
        7
    holinhot  
    OP
       2021-09-19 18:11:24 +08:00
    @Trim21 这算 BUG 吧
    holinhot
        8
    holinhot  
    OP
       2021-09-19 18:20:32 +08:00
    @Trim21 加了代理。request.Request(好像不行啊
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5424 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 05:48 · PVG 13:48 · LAX 22:48 · JFK 01:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.