gpt4 book ai didi

Python STDOUT 使用 openssl 子进程归档

转载 作者:太空宇宙 更新时间:2023-11-03 13:23:14 25 4
gpt4 key购买 nike

我正在尝试编写一个 python 脚本来自动执行通过 openSSL 检查 SSL 重新协商并将结果输出到文件的过程。我遇到了 2 个问题。

我的第一个问题是初始握手的输出被写入文件,但实际的重新协商部分没有。它显示在控制台上。

subprocess.call("echo \"R\" | openssl s_client -connect example.com:443", 
shell=True, stdout=FILE)

我的另一个问题(虽然这可能是错误的地方)是我无法使用 openSSL 命令来发送 GET 命令。

subprocess.call("echo -e \"GET / HTTP/1.1\r\n\r\n\" | openssl s_client -connect
example.com:443", shell=True)

同样,初始连接已建立,但 openSSL 存在,它不处理 GET 请求。

如有任何帮助,我们将不胜感激。谢谢。

最佳答案

没有理由使用 shell=True 作为输入。相反,请使用 stdin=subprocess.PIPE。另请注意,您的请求无效,因为 HTTP 1.1 需要 Host header 。此外,我想不出使用命令行 openssl 而不是 ssl module 的理由。 .

话虽这么说,但这是一个工作示例:

import subprocess

f = open('http_answer', 'w')
_,log = subprocess.Popen(
['openssl', 's_client', '-quiet', '-connect', 'twitter.com:443'],
stdout=f, stderr=subprocess.PIPE, stdin=subprocess.PIPE
).communicate('GET / HTTP/1.0\r\n\r\n')
print('Output of SSL:\n' + log)

关于Python STDOUT 使用 openssl 子进程归档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6710574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com