4

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 2

5

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
Sign up to request clarification or add additional context in comments.

2 Comments

>>> 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'
I think the first line of code should be urllib2.urlopen("http://google.com").getcode()
0

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)

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.