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

前端 未结 5 1832
别那么骄傲
别那么骄傲 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:33

    You can catch all exceptions and log what's get caught:

     import sys
     import traceback
     def formatExceptionInfo(maxTBlevel=5):
         cla, exc, trbk = sys.exc_info()
         excName = cla.__name__
         try:
             excArgs = exc.__dict__["args"]
         except KeyError:
             excArgs = ""
         excTb = traceback.format_tb(trbk, maxTBlevel)
         return (excName, excArgs, excTb)
     try:
         x = x + 1
     except:
         print formatExceptionInfo()
    

    (Code from http://www.linuxjournal.com/article/5821)

    Also read documentation on sys.exc_info.

提交回复
热议问题