首页 > 解决方案 > p.stdout.readline() 在 True 时中断:

问题描述

如果进程已停止写入行(这意味着程序已崩溃,在这种特定情况下,因为该进程将始终打印行),我正在尝试关闭该进程。

但是当我这样做line = proc.stdout.readline().decode("utf-8")然后进程挂起时没有打印任何东西到stdout,stderr,然后我的while循环没有被循环。我想这样做,所以我可以检测ffuf何时挂起,然后在10秒后进程持续挂起我会杀掉进程

tl;博士#If there is no more lines being printed by ffuf, the while loop will not get hit even if go_next_domain is True

with open("riot.txt") as f:
    for f_line in f:
        f_line = f_line.rstrip("\n\r")
        proc = subprocess.Popen(['ffuf', f_line], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        go_next_domain = False
        start_time = time.time()
        last_line_recieved = time.time()

        print("Bruteforcing", f_line)

        while not go_next_domain:
            print(proc.stdout.readable())
            line = proc.stdout.readline().decode("utf-8")

标签: pythonpython-3.xwhile-loopsubprocess

解决方案


这不是我正在寻找或想要的答案,更多的是解决方法,直到我找到更好的答案或有人提供了答案。只需在 stdoud/stderr 收到字符串时用当前时间更新一个变量,如果 5 秒内没有收到字符串,它将杀死程序。

proc = None
last_line_recieved = time.time()
go_next_domain = None

def kill_process():
    global go_next_domain
    while not go_next_domain:
        if time.time() - last_line_recieved > 5:
            proc.kill()
            print("Killing proccess...")
            go_next_domain = True

threading.Thread(target=kill_process).start()

with open("riot.txt") as f:
    for f_line in f:
        f_line = f_line.rstrip("\n\r")
        proc = subprocess.Popen(['ffuf', f_line], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        go_next_domain = False
        start_time = time.time()
        last_line_recieved = time.time()

        print("Bruteforcing", f_line)

        while not go_next_domain:
            line = proc.stdout.readline().decode("utf-8")

推荐阅读