如何使用urllib2获取使用SSLv3加密的网页

发布于 2024-12-14 06:37:06 字数 358 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情释 2024-12-21 06:37:06

我意识到这个响应已经晚了几年,但我也遇到了同样的问题,并且不想依赖 libcurl 安装在我运行它的机器上。希望这对将来发现这篇文章的人有用。

问题是 httplib.HTTPSConnection.connect 无法指定 SSL 上下文或版本。您可以在开始执行脚本之前覆盖此函数,以获得快速解决方案。

一个重要的考虑因素是,如上所述,此解决方法不会验证服务器证书的有效性。

import httplib
import socket
import ssl
import urllib2

def connect(self):
    "Connect to a host on a given (SSL) port."

    sock = socket.create_connection((self.host, self.port),
                                    self.timeout, self.source_address)
    if self._tunnel_host:
        self.sock = sock
        self._tunnel()

    self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file, ssl_version=ssl.PROTOCOL_TLSv1)

httplib.HTTPSConnection.connect = connect

opener = urllib2.build_opener()
f = opener.open('https://www.google.com/')

*注意:这个备用 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.

import httplib
import socket
import ssl
import urllib2

def connect(self):
    "Connect to a host on a given (SSL) port."

    sock = socket.create_connection((self.host, self.port),
                                    self.timeout, self.source_address)
    if self._tunnel_host:
        self.sock = sock
        self._tunnel()

    self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file, ssl_version=ssl.PROTOCOL_TLSv1)

httplib.HTTPSConnection.connect = connect

opener = urllib2.build_opener()
f = opener.open('https://www.google.com/')

*Note: this alternate connect() function was copy/pasted from httplib.py, and simply modified to specify the ssl_version in the wrap_socket() call

寄与心 2024-12-21 06:37:06

只要您在服务器上安装了 SSL 库<,就应该自动处理 SSL /a> (即您不必专门将其添加为处理程序)

http://docs.python.org/library/urllib2.html#urllib2.build_opener

如果Python安装支持SSL(即可以导入ssl模块),也会添加HTTPSHandler。

另外,请注意urlliburllib2< /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)

http://docs.python.org/library/urllib2.html#urllib2.build_opener

If the Python installation has SSL support (i.e., if the ssl module can be imported), HTTPSHandler will also be added.

Also, note that urllib and urllib2 have been merged in python 3 so their approach is a little different

你是暖光i 2024-12-21 06:37:06

由于我无法使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文