Set host header when using Python and urllib2

I use my own resolver and would like to use urllib2 to connect to IP (without permission in urllib2), and I would like to configure the HTTP Host header myself. But urllib2 just ignores my host header:

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

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

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

All Articles