使用Python和urllib2时设置Host-header [英] Set Host-header when using Python and urllib2

查看:281
本文介绍了使用Python和urllib2时设置Host-header的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自己的解析器,并希望使用urllib2来连接到IP(在urllib2中没有解析),我想自己设置HTTP Host-header。但是urllib2只是忽略了我的Host-header:

I'm using my own resolver and would like to use urllib2 to just connect to the IP (no resolving in urllib2) and I would like set the HTTP Host-header myself. But urllib2 is just ignoring my Host-header:

txheaders = { 'User-Agent': UA, "Host: ": nohttp_url }
robots = urllib2.Request("http://" + ip  + "/robots.txt", txdata, txheaders)


推荐答案

您已在主机中包含 string。

You have included ": " in the "Host" string.

txheaders = { "User-Agent": UA, "Host": nohttp_url }
robots = urllib2.Request("http://" + ip  + "/robots.txt", txdata, txheaders)

这篇关于使用Python和urllib2时设置Host-header的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆