2012-12-18 4 views
-1

나는 Python을 더 배우려고하지만로드 블록을 쳤습니다. urllib2/urllib가 제대로 작동하지 않습니다.Python을 배우는 동안로드 블록이 발생 함 - Urlopen 오류 10060

Traceback (most recent call last): 
    File "<pyshell#6>", line 1, in <module> 
    response = urllib2.urlopen('http://python.org/') 
    File "C:\Python27\lib\urllib2.py", line 126, in urlopen 
    return _opener.open(url, data, timeout) 
    File "C:\Python27\lib\urllib2.py", line 400, in open 
    response = self._open(req, data) 
    File "C:\Python27\lib\urllib2.py", line 418, in _open 
    '_open', req) 
    File "C:\Python27\lib\urllib2.py", line 378, in _call_chain 
    result = func(*args) 
    File "C:\Python27\lib\urllib2.py", line 1207, in http_open 
    return self.do_open(httplib.HTTPConnection, req) 
    File "C:\Python27\lib\urllib2.py", line 1177, in do_open 
    raise URLError(err) 
URLError: <urlopen error [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond> 

f=urllib.urlopen(URL) 다음 URL = "http://173.194.75.94" 같은 무언가조차이 같은 오류를 제공, 그래서는 DNS의 문제가 아니다 :이 response = urllib2.urlopen('http://python.org/')을한다면 나는이 오류가 발생합니다. 나는 프록시 나 방화벽 뒤에 있지 않습니다. 내 Windows 기본 방화벽을 제외하고 항상 비활성화 된 것처럼 작동하지 않습니다. 그렇다하더라도 파이썬과 pythonw.exe를 예외로 추가했습니다.

실마리를 해결하는 방법은 무엇입니까?

+1

일부 디버깅을 시도하십시오. 자신의 컴퓨터를 사용하여 사이트에 핑 (ping)을 보냅니다. 파이썬을 사용하여 자신의 컴퓨터에 대한 하위 프로세스를 만들고 사이트에 ping을 보냅니다. 이게 문제가되는 파이썬이된다면 말이야. 즉, 파이썬에는 네트워크에 액세스 할 수있는 권한이 없습니다. imho –

+0

분명히 방화벽이나 프록시 뒤에 있습니다 ..... –

답변

1

requests을 사용하면 훨씬 쉽게 생활 할 수 있습니다.

In [1]: import requests 

In [2]: pysite = requests.get('http://python.org/') 

In [3]: pysite.status_code 
Out[3]: 200 

In [4]: pysite.ok 
Out[4]: True 

In [5]: pysite.text[0:40] 
Out[5]: u'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML' 

In [6]: pysite.error 

In [7]: pysite.links 
Out[7]: {} 

In [8]: pysite.reason 
Out[8]: 'OK' 

예를 들어 다음과 같은 사이트로 이동할 때 예외가 발생합니다. 존재하지 않거나 네트워크에서 차단 :

In [3]: foobar = requests.get('http://foo.bar/') 
--------------------------------------------------------------------------- 
ConnectionError       Traceback (most recent call last) 
<ipython-input-3-0917ff568fd4> in <module>() 
----> 1 foobar = requests.get('http://foo.bar/') 

/usr/local/lib/python2.7/site-packages/requests-0.14.1-py2.7.egg/requests/api.pyc in get(url, **kwargs) 
    63 
    64  kwargs.setdefault('allow_redirects', True) 
---> 65  return request('get', url, **kwargs) 
    66 
    67 

/usr/local/lib/python2.7/site-packages/requests-0.14.1-py2.7.egg/requests/safe_mode.pyc in wrapped(method, url, **kwargs) 
    37     r.status_code = 0 # with this status_code, content returns None 
    38     return r 
---> 39   return function(method, url, **kwargs) 
    40  return wrapped 

/usr/local/lib/python2.7/site-packages/requests-0.14.1-py2.7.egg/requests/api.pyc in request(method, url, **kwargs) 
    49 
    50  try: 
---> 51   return session.request(method=method, url=url, **kwargs) 
    52  finally: 
    53   if adhoc_session: 

/usr/local/lib/python2.7/site-packages/requests-0.14.1-py2.7.egg/requests/sessions.pyc in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, return_response, config, prefetch, verify, cert) 
    239 
    240   # Send the HTTP Request. 
--> 241   r.send(prefetch=prefetch) 
    242 
    243   # Return the response. 

/usr/local/lib/python2.7/site-packages/requests-0.14.1-py2.7.egg/requests/models.pyc in send(self, anyway, prefetch) 
    629 
    630    except socket.error as sockerr: 
--> 631     raise ConnectionError(sockerr) 
    632 
    633    except MaxRetryError as e: 

ConnectionError: [Errno 8] hostname nor servname provided, or not known 

그러나 ConnectionError 예외에 의해 제공되는 정보는 무슨 일이 일어나고 있는지를 알려줍니다;

In [8]: try:      
    foobar = requests.get('http://foo.bar/') 
except requests.ConnectionError as oops: 
    print oops.message 
    ...:  
[Errno 8] hostname nor servname provided, or not known 
관련 문제