What errors/exceptions do I need to handle with urllib2.Request / urlopen?

前端 未结 5 1811
别那么骄傲
别那么骄傲 2020-12-04 09:28

I have the following code to do a postback to a remote URL:

request = urllib2.Request(\'http://www.example.com\', postBackData, { \'User-Agent\' : \'My User          


        
5条回答
  •  粉色の甜心
    2020-12-04 10:22

    $ grep "raise" /usr/lib64/python/urllib2.py
    IOError); for HTTP errors, raises an HTTPError, which can also be
            raise AttributeError, attr
                    raise ValueError, "unknown url type: %s" % self.__original
            # XXX raise an exception if no one else should try to handle
            raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
            perform the redirect.  Otherwise, raise HTTPError if no-one
                raise HTTPError(req.get_full_url(), code, msg, headers, fp)
                    raise HTTPError(req.get_full_url(), code,
                raise HTTPError(req.get_full_url(), 401, "digest auth failed",
                    raise ValueError("AbstractDigestAuthHandler doesn't know "
                raise URLError('no host given')
                raise URLError('no host given')
                raise URLError(err)
            raise URLError('unknown url type: %s' % type)
            raise URLError('file not on local host')
                raise IOError, ('ftp error', 'no host given')
                raise URLError(msg)
                raise IOError, ('ftp error', msg), sys.exc_info()[2]
                raise GopherError('no host given')
    

    There is also the possibility of exceptions in urllib2 dependencies, or of exceptions caused by genuine bugs.

    You are best off logging all uncaught exceptions in a file via a custom sys.excepthook. The key rule of thumb here is to never catch exceptions you aren't planning to correct, and logging is not a correction. So don't catch them just to log them.

提交回复
热议问题