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) )
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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
HTTPErrorto check for HTTP specific errors, and then useURLErrorto check for other problems:Of course, you’ll probably want to do something more clever than just printing the error codes, but you get the idea.