2012-12-18 17 views
-1

我試圖進一步學習Python,但是我遇到了障礙。我無法使urllib2/urllib正常工作。如果我這樣做response = urllib2.urlopen('http://python.org/')我得到這個錯誤:學習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> 

即使像URL = "http://173.194.75.94"其次是f=urllib.urlopen(URL)給出了同樣的錯誤,所以它不是一個DNS問題。 我落後於沒有代理或防火牆。除了我的Windows默認防火牆,它是一直被禁用的。即使如此,我添加了python和pythonw.exe到它是例外。

任何線索如何解決這個問題?

+1

嘗試一些調試。使用自己的計算機向該站點發送ping。然後使用python爲您自己的計算機創建一個子進程,並向該站點發送一個ping。如果這工作,那麼它的python就是問題所在。這意味着python沒有權限訪問網絡。 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