2017-10-17 258 views
0

我嘗試做以下請求模塊:的Python:我如何使用的urllib或從企業域(防火牆,代理服務器,cntlm等)

from urllib.request import urlopen 
data = urlopen("https://www.duolingo.com/users/SaifullahS6").read() 

我收到以下錯誤:

URLError: <urlopen error [WinError 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> 

同樣,當我試試這個:

import requests 
session = requests.Session() 
data = {"login": "SaifullahS6", "password": "mypassword"} 
req = requests.Request('POST', "https://www.duolingo.com/login", data=data,   
cookies=session.cookies) 
prepped=req.prepare() 
returned = session.send(prepped) 

我得到:

ConnectionError: HTTPSConnectionPool(host='www.duolingo.com', port=443): Max retries exceeded with url: /login (Caused by NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x000000000E6948D0>: Failed to establish a new connection: [WinError 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',)) 

我不知道如何提供我的互聯網連接的詳細信息。

  • 我在工作,我知道我們有一個公司代理。
  • 我們必須打開Windows防火牆,但我已經檢查了Python和pythonw在控制面板中的「域」列中勾選允許程序通過防火牆。
  • 當我從命令shell ping google.co.uk時,所有四個請求超時,但我可以從瀏覽器訪問它。
  • 在Internet選項控制面板,我點擊連接選項卡,然後LAN設置,我有「自動檢測設置」開啓,並且也「使用代理服務器爲LAN」,「地址」爲「本地主機「和」港口「是3128.這是cntlm。我建立了一次下載python軟件包,它似乎仍然是活躍的,因爲我剛剛設法更新我的一個軟件包。

我甚至都不需要直接回答我的問題;在這一點上,我只是想澄清一下在幕後實際發生的事情。任何幫助非常感謝!

回答

0

對於上述(urllib的模塊)第一種情況,我解決它由data = urlopen(...).read()線之前插入下列行:

proxies = { "http": "localhost:3128", 
      "https": "localhost:3128"} 
proxy = urllib.request.ProxyHandler(proxies) 
opener = urllib.request.build_opener(proxy) 
urllib.request.install_opener(opener) 

對於第二種情況(請求模塊),一切除最後相同行:

proxies = { "http": "localhost:3128", 
      "https": "localhost:3128"} 
returned = session.send(prepped, proxies=proxies) 

希望本筆記可以幫助其他人遇到此頁面。

相關問題