网址关闭时出现连接错误

时间:2017-09-19 05:09:54

标签: python-3.x

追踪(最近一次通话):   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ requests \ packages \ urllib3 \ connectionpool.py",第544行,在urlopen中     body = body,headers = headers)   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ requests \ packages \ urllib3 \ connectionpool.py",第349行,在_make_request中     conn.request(方法,网址,** httplib_request_kw)   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ http \ client.py",第1088行,请求中     self._send_request(方法,网址,正文,标题)   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ http \ client.py",第1126行,在_send_request中     self.endheaders(体)   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ http \ client.py",第1084行,在endheaders中     self._send_output(MESSAGE_BODY)   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ http \ client.py",第922行,在_send_output中     self.send(MSG)   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ http \ client.py",第857行,发送     self.connect()   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ requests \ packages \ urllib3 \ connection.py",第155行,在连接中     conn = self._new_conn()   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ requests \ packages \ urllib3 \ connection.py",第134行,在_new_conn     (self.host,self.port),self.timeout,** extra_kw)   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ requests \ packages \ urllib3 \ util \ connection.py",第88行,在create_connection中     提出错误   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ requests \ packages \ urllib3 \ util \ connection.py",第78行,在create_connection中     sock.connect(SA) ConnectionRefusedError:[WinError 10061]无法建立连接,因为目标计算机主动拒绝它

在处理上述异常期间,发生了另一个异常:

追踪(最近一次通话):   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ requests \ adapters.py",第370行,发送     超时=超时   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ requests \ packages \ urllib3 \ connectionpool.py",第597行,在urlopen     _stacktrace = sys.exc_info()[2])   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ requests \ packages \ urllib3 \ util \ retry.py",第245行,增量     提高six.reraise(类型(错误),错误,_stacktrace)   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ requests \ packages \ urllib3 \ packages \ six.py",309行,重新加载     提高value.with_traceback(tb)   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ requests \ packages \ urllib3 \ connectionpool.py",第544行,在urlopen中     body = body,headers = headers)   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ requests \ packages \ urllib3 \ connectionpool.py",第349行,在_make_request中     conn.request(方法,网址,** httplib_request_kw)   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ http \ client.py",第1088行,请求中     self._send_request(方法,网址,正文,标题)   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ http \ client.py",第1126行,在_send_request中     self.endheaders(体)   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ http \ client.py",第1084行,在endheaders中     self._send_output(MESSAGE_BODY)   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ http \ client.py",第922行,在_send_output中     self.send(MSG)   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ http \ client.py",第857行,发送     self.connect()   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ requests \ packages \ urllib3 \ connection.py",第155行,在连接中     conn = self._new_conn()   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ requests \ packages \ urllib3 \ connection.py",第134行,在_new_conn     (self.host,self.port),self.timeout,** extra_kw)   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ requests \ packages \ urllib3 \ util \ connection.py",第88行,在create_connection中     提出错误   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ requests \ packages \ urllib3 \ util \ connection.py",第78行,在create_connection中     sock.connect(SA) requests.packages.urllib3.exceptions.ProtocolError :('连接已中止。',ConnectionRefusedError(10061,'无法建立连接,因为目标计算机主动拒绝它',无,10061 ,没有))

在处理上述异常期间,发生了另一个异常:

追踪(最近一次通话):   文件" C:\ Users \ SD37561 \ Desktop \ python scripts \ link_monitor.py",第50行,in     mail_alert = validate_sites(sample_dict)   文件" C:\ Users \ SD37561 \ Desktop \ python scripts \ link_monitor.py",第20行,在validate_sites中     for _site,_auth in sample_dict.items()]   文件" C:\ Users \ SD37561 \ Desktop \ python scripts \ link_monitor.py",第20行,in     for _site,_auth in sample_dict.items()]   文件" C:\ Users \ SD37561 \ Desktop \ python scripts \ link_monitor.py",第14行,在site_is_reachable中     response = requests.get(site_url,auth = authentication)   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ requests \ api.py",第69行,获取     退货请求(' get',url,params = params,** kwargs)   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ requests \ api.py",第50行,请求中     response = session.request(method = method,url = url,** kwargs)   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ requests \ sessions.py",第465行,请求中     resp = self.send(prep,** send_kwargs)   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ _comtions \ sessions.py",第573行,发送     r = adapter.send(request,** kwargs)   文件" C:\ Users \ SD37561 \ AppData \ Local \ Continuum \ Anaconda3 \ lib \ site-packages \ requests \ adapters.py",第415行,发送     引发ConnectionError(错误,请求=请求) requests.exceptions.ConnectionError :('连接已中止。',ConnectionRefusedError(10061,'无法建立连接,因为目标计算机主动拒绝它',无,10061,无))

  
    
      

    
  

1 个答案:

答案 0 :(得分:1)

通过为dict遍历添加除Exception以及现在处理并打印用户友好消息来捕获错误