d-c
d-c

Reputation: 249

get http response codes with python

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?

Upvotes: 4

Views: 1540

Answers (2)

carl
carl

Reputation: 50534

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

Upvotes: 5

blaze
blaze

Reputation: 4364

Actually, httplib DOES allow to set User-Agent.

headers = { 'User-Agent' : 'someapp', 'Content-Type' : 'text/html' }  
conn = httplib.HTTPConnection(host, port)  
conn.request('POST', '/foobar', 'mydata', headers)

Upvotes: 0

Related Questions