add try catch with urllib2.urlopen

This commit is contained in:
苏世龙
2016-10-18 14:17:10 +08:00
parent 3ac95c785f
commit 1eebbf84ab
+15 -1
View File
@@ -247,7 +247,6 @@ class WebWeixin(object):
def webwxgetcontact(self): def webwxgetcontact(self):
SpecialUsers = self.SpecialUsers SpecialUsers = self.SpecialUsers
print self.base_uri
url = self.base_uri + '/webwxgetcontact?pass_ticket=%s&skey=%s&r=%s' % ( url = self.base_uri + '/webwxgetcontact?pass_ticket=%s&skey=%s&r=%s' % (
self.pass_ticket, self.skey, int(time.time())) self.pass_ticket, self.skey, int(time.time()))
dic = self._post(url, {}) dic = self._post(url, {})
@@ -1022,11 +1021,26 @@ class WebWeixin(object):
'ContentType', 'application/json; charset=UTF-8') 'ContentType', 'application/json; charset=UTF-8')
else: else:
request = urllib2.Request(url=url, data=urllib.urlencode(params)) request = urllib2.Request(url=url, data=urllib.urlencode(params))
#print(url)
#response = urllib2.urlopen(request)
try:
response = urllib2.urlopen(request) response = urllib2.urlopen(request)
data = response.read() data = response.read()
if jsonfmt: if jsonfmt:
return json.loads(data, object_hook=_decode_dict) return json.loads(data, object_hook=_decode_dict)
return data 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): def _xiaodoubi(self, word):
url = 'http://www.xiaodoubi.com/bot/chat.php' url = 'http://www.xiaodoubi.com/bot/chat.php'