Python 中的 HTTPS 会话重用

发布于 2024-11-17 13:19:51 字数 238 浏览 2 评论 0原文

我希望能够对 HTTPS 服务器使用并行请求。目前,我正在使用 PyCURL,但它无法在不同句柄之间重用相同的 SSL 会话 ID,并且每个句柄每次只能处理一次下载/上传。

考虑到协商需要时间(特别是因为使用了客户端证书),重用 id(就像浏览器从 Web 并行下载少量资源时所做的那样)可能会提高性能。

那么,现在有人知道 PyCURL 的解决方法,或者支持它的替代 HTTP 模块吗? httplib 似乎也没有完成这项工作。

I would like to be able to use parallel requests to a HTTPS server. Currently, I am using PyCURL, but it isn't able of reusing the same SSL session ID between different handles, and each handle can only take care of one download/upload each time.

Taking into account the negotiation takes time (specially because client certificate is used), reusing the id (as browsers do for downloading few resources in parallel from a web) that would probably improve the performance.

So, does anybody now about some workaround for PyCURL, or an alternative HTTP module that supports that? httplib doesn't seem to do the work, either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

云淡风轻 2024-11-24 13:19:51

正如此处所述,目前无法轻松访问重用会话 ID,我还没有听说过任何简单的解决方案这 - 但这应该只是在初次握手后保存上下文并重用它的问题。

PyOpenSSL 公开了这些机制,但级别比大多数人想要的要低。我会把钱花在以下一系列事件上:

  • 首先弄清楚如何进行会话重用,然后进行概念验证。一个有用的工具是 openssl 二进制文件(人们通常用来制作 SSL 密钥的工具)。它有一个内置客户端,您可以像这样使用:

openssl s_client -connect 主机:443

  • 您可以打印出各种好的诊断内容(例如您的 SSL 会话 ID),以便您可以在当前问题的范围之外验证它。

  • 一旦您了解了这一点,httplib 的 SSL 支持就非常简单了,HTTPSConnection 是 HTTPConnection 的一个非常薄的包装器(只有两个扩展该类的方法。您要修改的一个是 connect

httplib.py - HTTPSConnection 类

    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)

Reusing session ids isn't currently easily accessible as noted here, I haven't heard of any simple solution for this - but it should just be a matter of saving your context after the initial handshake and reusing it.

PyOpenSSL exposes these mechanisms, but at a lower level than most people would want. I'd put my money on the following sequence of events:

  • Figure out how to do the session resuse stuff first, just do a proof of concept. A useful tool for this is the openssl binary (the one people generally use to make SSL keys). It's got a built-in client that you can use like this:

openssl s_client -connect HOST:443

  • You can print out all sorts of good diagnostic stuff (such as your SSL session id) just so you can verify it outside of the scope of your immediate problem.

  • Once you have that, httplib's SSL support is pretty simple, HTTPSConnection is a very thin wrapper around HTTPConnection (only two methods extending the class. The one you want to modify is connect.

httplib.py - HTTPSConnection class

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