如何在python中捕获openssl的输出(How to capture the output of openssl in python)

编程入门 行业动态 更新时间:2024-10-11 11:18:26
如何在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_fingerprint

You 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

更多推荐

本文发布于:2023-08-02 18:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1379768.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   python   openssl   capture   output

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!