Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python urllib2 URLError HTTP status code.

I want to grab the HTTP status code once it raises a URLError exception:

I tried this but didn't help:

except URLError, e:
    logger.warning( 'It seems like the server is down. Code:' + str(e.code) )
like image 355
Hellnar Avatar asked Aug 12 '10 07:08

Hellnar


People also ask

What is URLError?

URLError. The handlers raise this exception (or derived exceptions) when they run into a problem. It is a subclass of OSError . reason. The reason for this error.

How do I use urllib2 in Python 3?

Simple urllib2 scripturlopen('http://python.org/') print "Response:", response # Get the URL. This gets the real URL. print "The URL is: ", response. geturl() # Getting the code print "This gets the code: ", response.

How does Python handle HTTP errors?

Just catch HTTPError , handle it, and if it's not Error 404, simply use raise to re-raise the exception. See the Python tutorial. can i do urllib2. urlopen("*") to handle any 404 errors and route them to my 404.

How do I increase http error in Python?

In the event of a network problem (e.g. DNS failure, refused connection, etc), Requests will raise a ConnectionError exception. In the event of the rare invalid HTTP response, Requests will raise an HTTPError exception. If a request times out, a Timeout exception is raised.


Video Answer


2 Answers

You shouldn't check for a status code after catching URLError, since that exception can be raised in situations where there's no HTTP status code available, for example when you're getting connection refused errors.

Use HTTPError to check for HTTP specific errors, and then use URLError to check for other problems:

try:     urllib2.urlopen(url) except urllib2.HTTPError, e:     print e.code except urllib2.URLError, e:     print e.args 

Of course, you'll probably want to do something more clever than just printing the error codes, but you get the idea.

like image 148
Pär Wieslander Avatar answered Sep 22 '22 01:09

Pär Wieslander


Not sure why you are getting this error. If you are using urllib2 this should help:

import urllib2 from urllib2 import URLError  try:     urllib2.urlopen(url) except URLError, e:     print e.code 
like image 45
Manoj Govindan Avatar answered Sep 20 '22 01:09

Manoj Govindan