3

从 Python 中,我想通过带有基本身份验证的 HTTPS 从网站检索内容。我需要磁盘上的内容。我在 Intranet 上,信任 HTTPS 服务器。平台是 Windows 上的 Python 2.6.2。

我一直在玩 urllib2,但到目前为止还没有成功。

我有一个正在运行的解决方案,通过 os.system() 调用 wget:

wget_cmd = r'\path\to\wget.exe -q -e "https_proxy = http://fqdn.to.proxy:port" --no-check-certificate --http-user="username" --http-password="password" -O path\to\output https://fqdn.to.site/content'

我想摆脱 os.system()。这在 Python 中可能吗?

4

3 回答 3

3

试试这个(请注意,您还必须填写服务器领域):

import urllib2
authinfo = urllib2.HTTPBasicAuthHandler()
authinfo.add_password(realm='Fill In Realm Here',
                      uri='https://fqdn.to.site/content',
                      user='username',
                      passwd='password')
proxy_support = urllib2.ProxyHandler({"https" : "http://fqdn.to.proxy:port"})
opener = urllib2.build_opener(proxy_support, authinfo)
fp = opener.open("https://fqdn.to.site/content")
open(r"path\to\output", "wb").write(fp.read())
于 2009-09-21T07:50:23.827 回答
3

代理和 https 在urllib2 上已经很长时间没有工作了。它将在 python 2.6 (v2.6.3) 的下一个发布版本中修复。

与此同时,您可以重新实现正确的支持,这就是我们为 mercurial 所做的:http: //hg.intevation.org/mercurial/crew/rev/59acb9c7d90f

于 2009-09-21T08:02:17.467 回答
0

你也可以试试这个: http ://code.google.com/p/python-httpclient/

(它还支持服务器证书的验证。)

于 2010-08-12T13:36:27.863 回答