2016-07-29 33 views
0

私はPythonを初めて使い、Python 3.5.0を使用しています。私は次のように簡単なコードを実装しようとしていた。python - urllib.error.URLError:<urlopenエラーがタイムアウトしました>

import urllib.request 
page = urllib.request.urlopen("http://www.google.com",timeout=20) 
text = page.read().decode("utf8") 
print(text) 

をしかし、予想外に、私は次のエラーを得ていた:私は私の大学のネットワークに接続されています

Traceback (most recent call last): 
    File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 1240, in do_open 
    h.request(req.get_method(), req.selector, req.data, headers) 
    File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 1083, in request 
    self._send_request(method, url, body, headers) 
    File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 1128, in _send_request 
    self.endheaders(body) 
    File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 1079, in endheaders 
    self._send_output(message_body) 
    File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 911, in _send_output 
    self.send(msg) 
    File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 854, in send 
    self.connect() 
    File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 826, in connect 
    (self.host,self.port), self.timeout, self.source_address) 
    File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\socket.py", line 707, in create_connection 
    raise err 
    File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\socket.py", line 698, in create_connection 
    sock.connect(sa) 
socket.timeout: timed out 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "D:\Python\Test_Run.py", line 2, in <module> 
    page = urllib.request.urlopen("http://www.google.com",timeout=20) 
    File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 162, in urlopen 
    return opener.open(url, data, timeout) 
    File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 465, in open 
    response = self._open(req, data) 
    File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 483, in _open 
    '_open', req) 
    File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 443, in _call_chain 
    result = func(*args) 
    File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 1268, in http_open 
    return self.do_open(http.client.HTTPConnection, req) 
    File "C:\Users\Dell\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 1242, in do_open 
    raise URLError(err) 
urllib.error.URLError: <urlopen error timed out> 

。それが私がこれを手に入れた理由ですか?これについて私は何ができますか?

+0

私の家のコンピュータでコードを試したところ、うまくいきました。問題はあなたの学校のネットワークにあるはずです。 'timeout = 20'を使わずに試してください – atayenel

+0

ええ、私はタイムアウトなしで試しました。しかし、時間がかかり、同じエラーが発生します。 –

+0

私はあなたのブラウザがプロキシサーバを使用するように設定されていると推測しますが、これによりGoogleにアクセスできるようになりますが、直接HTTP接続が許可されない(ある時点でファイアウォールによって落とされる) – twalberg

答えて

0
MAX_RETRY = 5 

def get_html(html_url, timeout=10, decode='utf-8'): 
    for tries in range(MAX_RETRY): 
     try: 
      with urllib.request.urlopen(html_url, timeout=timeout) as response: 
       return response.read().decode(decode) 
     except Exception as e: 
      logging.warning(str(e) + ',html_url:{0}'.format(html_url)) 
      if tries < (MAX_RETRY - 1): 
       continue 
      else: 
       print('Has tried {0} times to access url {1}, all failed!'.format(MAX_RETRY, html_url)) 
       return None 
関連する問題