Python:如何使用 urllib 或从公司域(防火墙、代理、cntlm 等)请求模块 [英] Python: How can I use urllib or requests modules from a corporate domain (firewall, proxy, cntlm etc)
问题描述
我正在尝试执行以下操作:
I am trying to do the following:
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>
同样,当我尝试这个时:
Similarly, when I try this:
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',))
我不知道如何提供我的互联网连接的详细信息.
I am not sure how to give details of my internet connection.
- 我在上班,我知道我们有公司代理.
- 我们打开了 Windows 防火墙,但我检查了在控制面板的域"列中是否勾选了 python 和 pythonw,以允许程序通过防火墙.
- 当我从命令 shell ping google.co.uk 时,所有四个请求都超时,但我可以从浏览器访问它.
- 在Internet 选项"控制面板中,单击连接"选项卡,然后单击局域网设置",并打开自动检测设置"和为局域网使用代理服务器",地址"为本地主机""和"端口"是3128.这是cntlm.我设置了一次来下载 python 包,它似乎仍然处于活动状态,因为我刚刚设法更新了我的一个包.
我什至不需要直接回答我的问题;在这一点上,我将就幕后实际发生的事情进行一些澄清.非常感谢任何帮助!
I don't even need a direct answer to my question; at this point I'll just settle for some clarity on what is actually going on behind the scenes. Any help much appreciated!
推荐答案
对于上面的第一种情况(urllib 模块),我通过在 data = urlopen(...).read 之前插入以下几行来解决它()
行:
For the first case above (urllib module), I solved it by inserting the following lines before the data = urlopen(...).read()
line:
proxies = { "http": "localhost:3128",
"https": "localhost:3128"}
proxy = urllib.request.ProxyHandler(proxies)
opener = urllib.request.build_opener(proxy)
urllib.request.install_opener(opener)
对于第二种情况(请求模块),除了最后一行之外,一切都一样:
For the second case (requests module), everything was the same except the last line:
proxies = { "http": "localhost:3128",
"https": "localhost:3128"}
returned = session.send(prepped, proxies=proxies)
希望这篇笔记能帮助其他遇到此页面的人.
Hope this note helps others who come across this page.
这篇关于Python:如何使用 urllib 或从公司域(防火墙、代理、cntlm 等)请求模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!