标签:www request urlopen else success color 开始 pen perror
1 #捕获异常 2 import urllib.request 3 import urllib.error 4 5 try: 6 response = urllib.request.urlopen(‘http://sasd.com‘) 7 except urllib.error.URLError as e: 8 print(‘挂掉的情形是:‘,e.reason)
1 import urllib.request 2 import urllib.error 3 try: 4 response = urllib.request.urlopen(‘http://www.asdddwf.com‘) 5 except urllib.error.HTTPError as e: 6 print(e.reason,e.code,e.headers,sep=‘\n‘) 7 except urllib.error.URLError as e: 8 print(e.reason) 9 else: 10 print(‘Requset Successfully‘) 11 #urllib.error的异常分类 12 #URlError,下面有个reason 13 #URLError的子类是HTTPError,下面有code,reason,headers 14 #捕捉异常要把子类开始捕捉,因为一旦捕捉第一个,后面的异常判断就不会继续执行了
1 #验证异常的具体原因 2 import socket 3 import urllib.request 4 import urllib.error 5 6 try: 7 response = urllib.request.urlopen(‘http://www.baidu.com‘,timeout=0.01) 8 except urllib.error.URLError as e: 9 print(e.reason) 10 if isinstance(e.reason,socket.timeout): 11 print(‘TIME OUT!‘)
标签:www request urlopen else success color 开始 pen perror
原文地址:http://www.cnblogs.com/themost/p/6842130.html