Python:如何使用urllib或从公司域(防火墙,代理,cntlm等)请求模块

时间:2017-10-17 14:36:15

标签: python proxy python-requests urllib

我正在尝试执行以下操作:

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选项”控制面板中,单击“连接”选项卡,然后单击“局域网”设置,打开“自动检测设置”,并“为局域网使用代理服务器”,“地址”为“localhost” “和”港口“是3128.这是cntlm。我设置了一次下载python软件包,它似乎仍然是活动的,因为我刚刚设法更新了我的一个软件包。

我甚至不需要直接回答我的问题;在这一点上,我只是清楚地了解幕后的实际情况。任何帮助非常感谢!

1 个答案:

答案 0 :(得分:1)

对于上面的第一个案例(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)

希望本说明能帮助遇到此页的其他人。