TimeoutError:[WinError 10060]和在Python3中使用urllib时的URLError

时间:2018-01-19 08:32:41

标签: python python-3.x timeout urllib

我正在尝试阅读和解析网站内容。我正在使用Python 3.5.2但我已经完成了300.000条目。在某些时候,我有两个错误;

    1.TimeoutError: [WinError 10060]
    2. AttributeError: type object 'URLError' has no attribute 'reason'

我搜索过这些错误并尝试了一些解决方案。但是,我仍然得到它们。我不知道如何解决第一个错误。对于第二个错误,它告诉我URLError中没有属性“reason”。但我也试过“代码”属性。除此之外,当我控制网页时,网页就存在了,为什么我会收到这些错误。如何解决其中两个错误?

    Error in pdb no chain added. Obj is empty. 3dzd
    <class 'urllib.error.URLError'>
    Traceback (most recent call last):
      File "C:\...\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 1254, in do_open
        h.request(req.get_method(), req.selector, req.data, headers)
      File "C:\...\AppData\Local\Programs\Python\Python35\lib\http\client.py", line 1106, in request
        self._send_request(method, url, body, headers)
      File "C:\...\AppData\Local\Programs\Python\Python35\lib\http\client.py", line 1151, in _send_request
        self.endheaders(body)
      File "C:\...\AppData\Local\Programs\Python\Python35\lib\http\client.py", line 1102, in endheaders
        self._send_output(message_body)
      File "C:\...\AppData\Local\Programs\Python\Python35\lib\http\client.py", line 934, in _send_output
        self.send(msg)
      File "C:\...\AppData\Local\Programs\Python\Python35\lib\http\client.py", line 877, in send
        self.connect()
      File "C:\...\AppData\Local\Programs\Python\Python35\lib\http\client.py", line 1252, in connect
        super().connect()
      File "C:\...\AppData\Local\Programs\Python\Python35\lib\http\client.py", line 849, in connect
        (self.host,self.port), self.timeout, self.source_address)
      File "C:\...\AppData\Local\Programs\Python\Python35\lib\socket.py", line 711, in create_connection
        raise err
      File "C:\...\AppData\Local\Programs\Python\Python35\lib\socket.py", line 702, in create_connection
        sock.connect(sa)
    TimeoutError: [WinError 10060] Bağlanılan uygun olarak belli bir süre içinde yanıt vermediğinden veya kurulan

    bağlantı bağlanılan ana bilgisayar yanıt vermediğinden bir bağlantı kurulamadı

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "C:\...\Google Drive\Scripts\4\script_4.py", line 146, in read_url_content
        data = list(urlopen("https://files.rcsb.org/view/"+pdb+".pdb", context = context ,timeout=60)) # it's a file like object and works just like a file
      File "C:\...\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 163, in urlopen
        return opener.open(url, data, timeout)
      File "C:\...\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 466, in open
        response = self._open(req, data)
      File "C:\...\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 484, in _open
        '_open', req)
      File "C:\...\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 444, in _call_chain
        result = func(*args)
      File "C:\...\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 1297, in https_open
        context=self._context, check_hostname=self._check_hostname)
      File "C:\...\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 1256, in do_open
        raise URLError(err)
    urllib.error.URLError: <urlopen error [WinError 10060] Bağlanılan uygun olarak belli bir süre içinde yanıt vermediğinden veya kurulan

    bağlantı bağlanılan ana bilgisayar yanıt vermediğinden bir bağlantı kurulamadı>

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "C:\...\Google Drive\Scripts\4\script_4.py", line 207, in <module>
        obj = read_url_content(pdb)
      File "C:\...\Google Drive\Scripts\4\script_4.py", line 156, in read_url_content
        print(e.reason)
    AttributeError: type object 'URLError' has no attribute 'reason'

我的代码如下;

    import requests
    import time
    from urllib.request import urlopen  # the lib that handles the url stuff
    import urllib.request, urllib.error, urllib.parse
    import ssl
    import sys
    from socket import timeout
    def read_url_content(pdb):
        pdbObj = None
        try:
            context = ssl._create_unverified_context()
            data = list(urlopen("https://files.rcsb.org/view/"+pdb+".pdb", context = context ,timeout=60)) # it's a file like object and works just like a file
            pdbObj = PDB(pdb)
        except urllib.error.URLError as e:
            print("Error in pdb no chain added. Obj is empty.", pdb)
            ResponseData = e.reason
            e = sys.exc_info()[0]
            print(e)
            print(e.reason)
            pass
        except timeout:
            logging.error('socket timed out - URL %s', pdb)
        return pdbObj

0 个答案:

没有答案