如何使用urllib2获取使用SSLv3加密的网页
我正在使用 python 2.7,我想获取需要 sslv3 的网页的内容。目前,当我尝试访问该页面时,出现错误 SSL23_GET_SERVER_HELLO,并且在网上进行的一些搜索引导我找到以下解决方案,该解决方案修复了 Python 3 中的问题
urllib.request.install_opener(urllib.request.build_opener(urllib.request.HTTPSHandler(context=ssl.SSLContext(ssl.PROTOCOL_TLSv1))))
如何在 python 2.7 中获得相同的效果,因为我似乎无法找到 HTTPSHandler 类的上下文参数的等效项。
I'm using python 2.7 and I'd like to get the contents of a webpage that requires sslv3. Currently when I try to access the page I get the error SSL23_GET_SERVER_HELLO, and some searching on the web lead me to the following solution which fixes things in Python 3
urllib.request.install_opener(urllib.request.build_opener(urllib.request.HTTPSHandler(context=ssl.SSLContext(ssl.PROTOCOL_TLSv1))))
How can I get the same effect in python 2.7, as I can't seem to find the equivalent of the context argument for the HTTPSHandler class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我意识到这个响应已经晚了几年,但我也遇到了同样的问题,并且不想依赖 libcurl 安装在我运行它的机器上。希望这对将来发现这篇文章的人有用。
问题是
httplib.HTTPSConnection.connect
无法指定 SSL 上下文或版本。您可以在开始执行脚本之前覆盖此函数,以获得快速解决方案。一个重要的考虑因素是,如上所述,此解决方法不会验证服务器证书的有效性。
*注意:这个备用
connect()
函数是从 httplib.py 复制/粘贴的,并简单修改为在wrap_socket()
中指定ssl_version
> 打电话I realize this response is a few years too late, but I also ran into the same problem, and didn't want to depend on libcurl being installed on a machine where I ran this. Hopefully, this will be useful to those who find this post in the future.
The problem is that
httplib.HTTPSConnection.connect
doesn't have a way to specify SSL context or version. You can overwrite this function before you hit the meat of your script for a quick solution.An important consideration is that this workaround, as discussed above, will not verify the validity of the server's certificate.
*Note: this alternate
connect()
function was copy/pasted from httplib.py, and simply modified to specify thessl_version
in thewrap_socket()
call只要您在服务器上安装了 SSL 库<,就应该自动处理 SSL /a> (即您不必专门将其添加为处理程序)
另外,请注意
urllib
和urllib2< /code> 已合并到 python 3 中,所以他们的方法有点不同
SSL should be handled automatically as long as you have the SSL libraries installed on your server (i.e. you shouldn't have to specificially add it as a handler)
Also, note that
urllib
andurllib2
have been merged in python 3 so their approach is a little different由于我无法使用 urllib2 来做到这一点,我最终屈服并转向使用 libCurl 绑定,就像@Bruno 在对 Pastylegs 答案的评论中建议的那样。
Since I was unable to do this using urllib2, I eventually gave in and moved to using the libCurl bindings like @Bruno had suggested in the comments to pastylegs answer.