如何在python中捕获openssl的输出(How to capture the output of openssl in python)
我试图在python中运行以下openssl命令:
cmd = "openssl x509 -sha1 -in esx.crt -noout -fingerprint" tmp = os.popen(cmd) tmp_sha1 = tmp.readline()该命令应该生成证书的指纹。 我试图通过文件对象捕获输出。 但是当我读到这个文件对象时,它里面什么都没有。 我在命令行上执行了这个命令,运行正常,生成指纹。 你能告诉我怎样才能获得指纹?
I am trying to run the following openssl command in python:
cmd = "openssl x509 -sha1 -in esx.crt -noout -fingerprint" tmp = os.popen(cmd) tmp_sha1 = tmp.readline()This command is supposed to generate a fingerprint of the certificate. I am trying to capture the output through the file object. But when I read this file object, there's nothing in it. I have executed this command on the command line and it runs fine, generates the fingerprint. Could you tell me how can I get the fingerprint?
最满意答案
您可以使用OpenSSL模块在Python中本地实现此目的。
from OpenSSL.crypto import load_certificate, FILETYPE_PEM cert_file_string = open("esx.crt", "rb").read() cert = load_certificate(FILETYPE_PEM, cert_file_string) sha1_fingerprint = cert.digest("sha1") print sha1_fingerprintYou achieve this natively within Python using the OpenSSL module.
from OpenSSL.crypto import load_certificate, FILETYPE_PEM cert_file_string = open("esx.crt", "rb").read() cert = load_certificate(FILETYPE_PEM, cert_file_string) sha1_fingerprint = cert.digest("sha1") print sha1_fingerprint更多推荐
发布评论