终止使用 subprocess 模块创建的 Python 进程一旦进程完成。

huangapple go评论59阅读模式
英文:

Terminating a Python process based on the subprocess module once the process is finished

问题

Overview: 我正在尝试开发一个基于Web的SSL扫描工具,其中作为后端,我使用Python代码和subprocess模块执行Ubuntu发行版中的testssl.sh脚本。

Doubt: 我编写的Python代码(如下所示)即使在testssl.sh脚本完成扫描服务器配置后仍然继续执行。如果有人能告诉我如何修改代码,以便在testssl.sh脚本执行完毕后停止代码执行,我将非常感激。

import subprocess

# 打开ubuntu.exe文件的实际路径并执行ubuntu.exe文件
process = subprocess.Popen(
    [r"E:\WSL\Ubuntu_2004\Ubuntu\Ubuntu_2004.2021.825.0_x64\ubuntu.exe"],
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE
)
# 更改存储testssl.sh脚本的目录
process.stdin.write(b"cd /mnt/e/WSL/Ubuntu_2004/testssl.sh-3.1dev\n")
process.stdin.flush()

# 从用户获取主机名
hostname = # input("Enter the Hostname to test: ")

# 使用主机名执行testssl.sh脚本,并将输出重定向到名为scans的HTML文件夹
command = f"./testssl.sh --htmlfile ../reports/scans --openssl-timeout 5 {hostname}\n"
process.stdin.write(command.encode())
process.stdin.flush()

# process.wait(timeout=****)

我尝试使用process.wait(timeout=****),但使用这种方法的问题是我无法确定超时的固定时间,可能会发生以下两种情况:

(1)如果服务器配置不太复杂且没有许多关联的IP地址,那么testssl.sh脚本最多会在5到7分钟内完成。然后,如果将超时值设置为大约800或900(秒),超过5到7分钟,程序会在VSCode中继续运行,然后在超时值之后抛出异常。

或者,

(2)如果服务器配置复杂且有多个关联的IP地址,那么testssl.sh脚本需要很长时间,即超过超时值(800或900秒)。然后,在控制台中还会出现超时异常,并且testssl.sh脚本在扫描服务器配置方面未能完全执行。

英文:

Overview: I am trying to develop a web-based SSL Scanner where as backend I am executing the testssl.sh script in the ubuntu distro by means of python code using the subprocess module.

Doubt: The python code that I have written (as mentioned below) keeps on executing even after the testssl.sh script has finished scanning a server configuration. I will be highly obliged if anyone could tell me as to how can I modify the code so that the code execution stops once the testssl.sh script has also finished execution.

As of now I am taking the input from the console as I am yet to work on the frontend and API development, and since it is my first time posting on this forum, I apologise for breaking any community guidelines unknowingly. And, I will be really grateful if anyone can help me with this issue as to how should I tackle this.

Code:

import subprocess

# Opens the actual path to the ubuntu.exe file and executes the ubuntu.exe file
process = subprocess.Popen(
    [r"E:\WSL\Ubuntu_2004\Ubuntu\Ubuntu_2004.2021.825.0_x64\ubuntu.exe"],
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE
)
# Change the directory where the testssl.sh script is stored
process.stdin.write(b"cd /mnt/e/WSL/Ubuntu_2004/testssl.sh-3.1dev\n")
process.stdin.flush()

# Get the Hostname from the user
hostname = # input("Enter the Hostname to test: ")

# Execute the testssl.sh script with the Hostname and redirect the output to a folder named scans as a HTML file

command = f"./testssl.sh --htmlfile ../reports/scans --openssl-timeout 5 {hostname}\n"
process.stdin.write(command.encode())
process.stdin.flush()

# process.wait(timeout=****)

I have tried using the process.wait(timeout=****), but the problem with this approach is that it is impossible for me to determine a fixed amount of timefor the timeout, and either of the two things happen:

(1) If the server configuration is not that complex and there are not many IP addresses associated then the testssl.sh script finishes within 5 to 7 minutes maximum. Thenif the timeout value is set to somewhat around 800 or 900 (seconds), which is more than 5 to 7 minutes then the program keeps on running in VSCode and after the timeout value it throws an exception.

OR,

(2) If the server configuration is complex and there multiple IP addresses associated then the testssl.sh script takes a lot of time i.e. more than the timeout(800 or 900 seconds) value. Then, in that I also get a timeout exception in the console and the testssl.sh script remains executed incompletely with respect to scanning the server configuration.

答案1

得分: 0

尝试从 process.stdout 读取,直到它到达 EOF(文件结束)。这意味着另一个进程已经结束。然后,你可以退出你的程序。

英文:

Have you tried reading from process.stdout until it EOFs? That will mean that the other process has ended. You then can exit your program.

huangapple
  • 本文由 发表于 2023年4月11日 16:44:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定