python - 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")
解决方案
这不是我正在寻找或想要的答案,更多的是解决方法,直到我找到更好的答案或有人提供了答案。只需在 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")
推荐阅读
- javascript - 如何将用户名从 django 中的用户模型传递到自动完成
- angularjs - 带有 angularJS 的 Webpack4 配置与 html 加载器问题
- angular - 通过设置 angular.json sourceMap 属性调试,Angular 7 生产构建项目
- c# - 如何使用 Edge Collider 2D 检测鼠标点击?
- node.js - Apollo Server Express + GraphQL 关系
- ios - 我用 CocoaPods 破坏了 GIT 并且无法合并/提交
- sql - 包含 XML 的拆分 Clob
- python-3.x - 如何使用 Python 查找并打开文件
- graphql - 将输入类型用于 graphql 查询是一种不好的做法吗?
- python - 无法从 Radiobutton 获取值