0

I'm trying to write a small program that will simply display the header information of a website. Here is the code:

import urllib2

url = 'http://some.ip.add.ress/'

request = urllib2.Request(url)

try:
    html = urllib2.urlopen(request)
except urllib2.URLError, e:
    print e.code
else:
    print html.info()

If 'some.ip.add.ress' is google.com then the header information is returned without a problem. However if it's an ip address that requires basic authentication before access then it returns a 401. Is there a way to get header (or any other) information without authentication?


I've worked it out.

After try has failed due to unauthorized access the following modification will print the header information:

print e.info()

instead of:

print e.code()

Thanks for looking :)

2
  • It's ok i've worked it out. print e.info() on error Still learning python.... Commented Apr 17, 2012 at 15:53
  • 2
    Post your solution as an answer to this question. Commented Apr 17, 2012 at 15:55

2 Answers 2

1

If you want just the headers, instead of using urllib2, you should go lower level and use httplib

import httplib
conn = httplib.HTTPConnection(host)
conn.request("HEAD", path)
print conn.getresponse().getheaders()
Sign up to request clarification or add additional context in comments.

5 Comments

I tired that before and received a 401. Your method works great! Thanks for that
I seem to be having problems again, the python console is returning [Errno 104] Connection reset by peer and i'm not sure what this means. I'm the peer but i'm not resetting anything, am i!?
Most likely means, that the other end is not accepting HTTP connections.
You'd think so but i can access and log in via my chrome browser just fine. Also my orginal code (at the top) works fine and returns the headers no problem! Straaaange
Instead of going low level I think it would be better to show how to do HEAD request using urllib2.
0

If all you want are HTTP headers then you should make HEAD not GET request. You can see how to do this by reading Python - HEAD request with urllib2.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.