I know how to do this with httplib, but I need to also set the user-agent and I'm sure you need urllib to do that. How can I get the http response codes with urllib?
2 Answers
You can use .getcode() in urllib2 to get the HTTP code:
urllib2.urlopen("http://google.com").getcode()
Full headers with are in info() as a list:
urllib2.urlopen("http://google.com").info().headers
2 Comments
d-c
>>> urllib2.urlopen("google.com").info().getcode() Traceback (most recent call last): File "<pyshell#43>", line 1, in <module> urllib2.urlopen("google.com").info().getcode() AttributeError: HTTPMessage instance has no attribute 'getcode'
Manoj Govindan
I think the first line of code should be
urllib2.urlopen("http://google.com").getcode()