From 1eebbf84ab8836f475888cd18a308e9abc041125 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E4=B8=96=E9=BE=99?= Date: Tue, 18 Oct 2016 14:17:10 +0800 Subject: [PATCH] add try catch with urllib2.urlopen --- weixin.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/weixin.py b/weixin.py index f5ae59b..39831aa 100755 --- a/weixin.py +++ b/weixin.py @@ -247,7 +247,6 @@ class WebWeixin(object): def webwxgetcontact(self): SpecialUsers = self.SpecialUsers - print self.base_uri url = self.base_uri + '/webwxgetcontact?pass_ticket=%s&skey=%s&r=%s' % ( self.pass_ticket, self.skey, int(time.time())) dic = self._post(url, {}) @@ -1022,11 +1021,26 @@ class WebWeixin(object): 'ContentType', 'application/json; charset=UTF-8') else: request = urllib2.Request(url=url, data=urllib.urlencode(params)) - response = urllib2.urlopen(request) - data = response.read() - if jsonfmt: - return json.loads(data, object_hook=_decode_dict) - return data + #print(url) + #response = urllib2.urlopen(request) + + try: + response = urllib2.urlopen(request) + data = response.read() + if jsonfmt: + return json.loads(data, object_hook=_decode_dict) + return data + except urllib2.HTTPError, e: + checksLogger.error('HTTPError = ' + str(e.code)) + except urllib2.URLError, e: + checksLogger.error('URLError = ' + str(e.reason)) + except httplib.HTTPException, e: + checksLogger.error('HTTPException') + except Exception: + import traceback + checksLogger.error('generic exception: ' + traceback.format_exc()) + + return '' def _xiaodoubi(self, word): url = 'http://www.xiaodoubi.com/bot/chat.php'