繁体   English   中英

如何使用python建立安全连接?

[英]How to make secure connection with python?

我正在使用python3。 我需要使用证书文件进行安全连接。 在这种情况下,我使用了来自http.client的Httpsconnection类。该类获取certs文件路径并使用它。 像这样:

 import http.client
 client=http.client.HTTPSConnection\
 ("epp.nic.ir",key_file="filepath\\nic.pem",cert_file="filepath\\nic.crt")

如您所见,该类获取文件的路径并且可以正常工作。 但是我需要提供这些文件的内容。 因为我想将crt文件和pem文件的内容放入DB。 原因是文件路径可能已更改...所以我尝试了以下方法:

import http.client
import base64

cert = b'''
content of cert file
'''
pem = b'''
content of pem file
'''
client=http.client.HTTPSConnection("epp.nic.ir" ,pem, cert)

如预期的那样,我收到此错误:

TypeError: certfile should be a valid filesystem path

有什么办法可以使此类获取文件内容而不是文件路径吗? 还是可以为此目的更改http的源代码?

可以修改Python源代码,但这不是推荐的方法,因为它肯定会带来可移植性,可维护性和其他问题。

  • 考虑到您要更新Python版本,因此每次更新时都必须应用您的修改。
  • 考虑您想在另一台机器上运行代码,同样是同样的问题。

除了修改源代码之外,还有一种更好的方法:扩展API。

您可以HTTPSConnection现有HTTPSConnection类,并通过自己的实现覆盖其构造函数方法。

有很多方法可以满足您的需求。

这是带有子类的可能解决方案:

import http.client
import tempfile

class MyHTTPSConnection(http.client.HTTPSConnection):
    """HTTPSConnection with key and cert files passed as contents rather than file names"""

    def __init__(self, host, key_content=None, cert_content=None, **kwargs):
        # additional parameters are also optional so that
        # so that this class can be used with or without cert/key files
        # as a replacement of standard HTTPSConnection
        self.key_file = None
        self.cert_file = None

        # here we write the content of cert & pem into a temporary file
        # delete=False keeps the file in the file system
        # but, this time we need to remove it manually when we are done
        if key_content:
            self.key_file = tempfile.NamedTemporaryFile(delete=False)
            self.key_file.write(key_content)
            self.key_file.close()
            # NamedTemporaryFile object provides 'name' attribute
            # which is a valid file name in the file system
            # so we can use those file names to initiate the actual HTTPSConnection
            kwargs['key_file'] = self.key_file.name

        # same as above but this time for cert content and cert file
        if cert_content:
            self.cert_file = tempfile.NamedTemporaryFile(delete=False)
            self.cert_file.write(cert_content)
            self.cert_file.close()
            kwargs['cert_file'] = self.cert_file.name


        # initialize super class with host and keyword arguments
        super().__init__(host, **kwargs)

    def clean(self):
        # remove temp files from the file system
        # you need to decide when to call this method
        os.unlink(self.cert_file.name)
        os.unlink(self.pem_file.name)

host = "epp.nic.ir"
key_content = b'''content of key file'''
cert_content = b'''content of cert file'''

client = MyHTTPSConnection(host, key_content=key_content, cert_content=cert_content)
# ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM