Python 2.6.1中的urllib2是否支持通过https进行代理

ste*_*anB 6 python https proxy urllib2

Python 2.6.1中的urllib2是否支持通过https进行代理?

我在http://www.voidspace.org.uk/python/articles/urllib2.shtml上找到了以下内容:

注意

目前,urllib2不支持通过代理获取https位置.这可能是个问题.

我正在尝试自动登录到网站并下载文档,我有有效的用户名/密码.

proxy_info = {
    'host':"axxx", # commented out the real data
    'port':"1234"  # commented out the real data
}

proxy_handler = urllib2.ProxyHandler(
                 {"http" : "http://%(host)s:%(port)s" % proxy_info})
opener = urllib2.build_opener(proxy_handler,
         urllib2.HTTPHandler(debuglevel=1),urllib2.HTTPCookieProcessor())
urllib2.install_opener(opener)

fullurl = 'https://correct.url.to.login.page.com/user=a&pswd=b' # example
req1 = urllib2.Request(url=fullurl, headers=headers)
response = urllib2.urlopen(req1)
Run Code Online (Sandbox Code Playgroud)

我已经让它适用于类似的页面,但没有使用HTTPS,我怀疑它没有通过代理 - 它只是像我没有指定代理时一样卡住.我需要通过代理出去.

我需要进行身份验证但不使用基本身份验证,urllib2会在通过https网站时找出身份验证(我通过网址向网站提供用户名/密码)?

编辑:没有,我测试过

   proxies = {
        "http" : "http://%(host)s:%(port)s" % proxy_info,
        "https" : "https://%(host)s:%(port)s" % proxy_info
    }

    proxy_handler = urllib2.ProxyHandler(proxies)
Run Code Online (Sandbox Code Playgroud)

我得到错误:

urllib2.URLError:urlopen错误[Errno 8] _ssl.c:480:违反协议发生EOF

Gri*_*ave 6

修复了Python 2.6.3和其他几个分支:


Ale*_*lli 3

我不确定您引用的 Michael Foord 的文章是否已更新到 Python 2.6.1 —— 为什么不尝试一下呢?不要像您现在所做的那样告诉 ProxyHandler 该代理仅适用于 http,也可以将其注册为 https(当然,您应该在调用 ProxyHandler 之前将其格式化为变量一次,然后在dict):这可能有效,也可能无效,但是,你甚至没有尝试,那肯定行不通!-)