2012-03-06 90 views
3

我試圖下載一個頁面的HTML(在這種情況下爲http://www.google.com),但我收到一個錯誤。這裏是我的交互提示會話:Python urllib.request.urlopen()返回錯誤10061?

Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win 
32 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import urllib 
>>> import urllib.request 
>>> html = urllib.request.urlopen("http://www.google.com") 
Traceback (most recent call last): 
    File "\\****.****.org\myhome\python\lib\urllib\request.py", line 1136, in 
do_open 
    h.request(req.get_method(), req.selector, req.data, headers) 
    File "\\****.****.org\myhome\python\lib\http\client.py", line 964, in req 
uest 
    self._send_request(method, url, body, headers) 
    File "\\****.****.org\myhome\python\lib\http\client.py", line 1002, in _s 
end_request 
    self.endheaders(body) 
    File "\\****.****.org\myhome\python\lib\http\client.py", line 960, in end 
headers 
    self._send_output(message_body) 
    File "\\****.****.org\myhome\python\lib\http\client.py", line 805, in _se 
nd_output 
    self.send(msg) 
    File "\\****.****.org\myhome\python\lib\http\client.py", line 743, in sen 
d 
    self.connect() 
    File "\\****.****.org\myhome\python\lib\http\client.py", line 721, in con 
nect 
    self.timeout, self.source_address) 
    File "\\****.****.org\myhome\python\lib\socket.py", line 404, in create_c 
onnection 
    raise err 
    File "\\****.****.org\myhome\python\lib\socket.py", line 395, in create_c 
onnection 
    sock.connect(sa) 
socket.error: [Errno 10061] No connection could be made because the target machi 
ne actively refused it 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "\\****.****.org\myhome\python\lib\urllib\request.py", line 138, in 
urlopen 
    return opener.open(url, data, timeout) 
    File "\\****.****.org\myhome\python\lib\urllib\request.py", line 369, in 
open 
    response = self._open(req, data) 
    File "\\****.****.org\myhome\python\lib\urllib\request.py", line 387, in 
_open 
    '_open', req) 
    File "\\****.****.org\myhome\python\lib\urllib\request.py", line 347, in 
_call_chain 
    result = func(*args) 
    File "\\****.****.org\myhome\python\lib\urllib\request.py", line 1156, in 
http_open 
    return self.do_open(http.client.HTTPConnection, req) 
    File "\\****.****.org\myhome\python\lib\urllib\request.py", line 1139, in 
do_open 
    raise URLError(err) 
urllib.error.URLError: <urlopen error [Errno 10061] No connection could be made 
because the target machine actively refused it> 
>>> 

我最好的猜測是,我的網絡防火牆阻止連接(最大/ 80以外的其他所有端口被封鎖)。雖然,我不明白爲什麼; Python不能連接80端口?有誰知道發生了什麼?

+0

也許您的解決方案是在這裏:http://stackoverflow.com/questions/5620263/using-an-http-代理 - 蟒蛇(因此,重複的問題) – 2012-03-06 16:09:25

+0

我沒有使用urllib2,這個問題是不同的,我們都有同樣的錯誤。 – 2012-03-06 16:10:58

回答

3

錯誤來自操作系統級別,與Python無關。這與任何編程語言都是一樣的。

請聯繫您的網絡管理員,看看有什麼事情,除非您自己解決防火牆問題。如果您需要HTTP代理,那麼您可以爲urllib配置一個。

+0

剛剛遇到這個問題我自己,值得知道在Windows中的http_proxy環境變量導致與python的urllib問題。刪除它修復了問題 – cudiaco 2014-06-17 10:52:28

8

的urllib需要從Internet Explorer,這通常是Tools->Internet Options->Connections->Lan Settings

如果你有一個代理下的代理設置,確保它是正確的IE瀏覽器,或set it when using urllib。如果您不使用代理瀏覽,請確保IE中的代理設置爲空。

幾個小時前我一直在努力解決這個問題,直到我意識到我曾經安裝的第三方應用程序在IE中改變了設置,而我卻在沒有代理的情況下在Firefox中愉快地瀏覽。


另一種解決方案是在the this link它展示瞭如何強制的urllib忽略IE的代理設置

+0

完美,它的工作:) – 2015-03-16 05:31:54