urllib2 won't raise exceptions

OtisUsenet otis_usenet at yahoo.com
Mon Jul 22 19:27:18 EDT 2002


Hello,

I am trying to use urllib2 to make HTTP POST requests and a) retrieve
HTTP status, and b) check for any exceptions.

1) I don't see a way to get the status code back from urllib nor
urllib2 (and I read that this will be impossible to get from them and
that httplib should be used instead).

2) I read that if something fails, URLError or HTTPError will be
raised.  So I have the code to catch them and print someting. 
However, nothing ever gets printed, even if I request pages that give
'500 Server Error', like in the case below.

Am I doing something wrong?
Thanks.
(pardon my indentation, I pasted this a few times...)

from urllib2 import *

def sendRequest(self):

         try:
             # NB: this URL gives "500 Server Error"
             urlObject = \
urllib2.urlopen('http://www.clr.utoronto.ca/cgi-bin/doc/show/doc=Cross.Rodney')
             response = urlObject.read()
             urlObject.close()
             return response
         except URLError, msg:
             print "URLError: ", msg;
         except HTTPError, msg:
             print "HTTPError: ", msg;
         except Exception, msg:
             print "EXCEPTION"
         except Error, e:
             print "ERROR"



More information about the Python-list mailing list