0

I just came across the term 'Host Header' and I believe that I understand it fairly well. I was wondering how I could find out the host header through the requests library. I would think that it would be as simple as setting a requests.get(<some url>) object as a variable called r and printing r.headers['host'] but the response headers do not indicate the host. This would mainly come handy for me when sending a get request to an ip address and not a domain name. Is it safe to assume that the host header is just the ip of the server that I am targeting?

I've tried this on google.com and stackoverflow.com

UCProgrammer
  • 517
  • 7
  • 21
  • 2
    `r.headers` is the _response_ headers, sent back by the server. You want `r.request.headers`, the _request_ headers that the library built to send to the server. – abarnert Sep 04 '18 at 18:59
  • thank you. that is correct. I still don't see a value for host in the headers though. What would the value for host be if I am making a get request for google.com? Would it just be google.com? – UCProgrammer Sep 04 '18 at 19:04
  • Yes, either that, or `google.com:80`. IIRC, the port is optional if it’s the default for the scheme, and the whole thing is optional in HTTP/1.0 (unless you’re talking to a virtual server, in which case it’s obviously necessary) but required in 1.1, but you might want to check the specs or MDN docs on that part. – abarnert Sep 04 '18 at 19:09
  • One more thing to look at: if you’re using HTTPS with its server name indication stuff, you might not need a Host header. I don’t know if that’s true (again, check the specs), but if it is, is bet the requests authors know that (Kenneth Reitz can cite all the RFCs in his sleep) and don’t add it. And I think Google and SO both redirect HTTP to HTTPS, right? – abarnert Sep 04 '18 at 19:50
  • @abarnert that is correct. Google does redirect. I apologize for the late reply. I will check on all that you mentioned. – UCProgrammer Sep 04 '18 at 22:25

0 Answers0