gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 17:12:19 24 4
gpt4 key购买 nike

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

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 使用代理服务器”,“地址”是“localhost” ”和“端口”是 3128。这是 cntlm。我设置了一次来下载 python 包,它似乎仍然处于事件状态,因为我刚刚设法更新了我的一个包。

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

最佳答案

对于上面的第一种情况(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)

对于第二种情况(requests 模块),除了最后一行,其他都一样:

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

希望这篇文章能帮助其他浏览此页面的人。

关于Python:如何使用 urllib 或从公司域(防火墙、代理、cntlm 等)请求模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46792756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com