繁体   English   中英

从子进程连续读取

[英]continuous read from subprocess.Popen

在Python中,我有一个关于subprocess.Popen函数的问题,我的问题是我无法继续读取stdout流。 当我在函数末尾使用communicate()时,会得到首选输出。 但是我这里有两个问题。 首先, communicate()在输出任何内容之前会缓冲整个输出,因此获得连续输出会很好。 其次,我已经阅读了communicate()文档,其中communicate()并非针对大数据流,在我的情况下就是这种情况。

#!/usr/bin/python

import os
import sys
from subprocess import *
import itertools


def combinate(hash_mode,hash_file,directory):
erg = Popen(['hashcat', '-a', '0', '-m', hash_mode, hash_file, '-O', '--potfile-disable'],
                     stdin=PIPE,
                     stdout=PIPE,
                     stderr=PIPE,
                     universal_newlines=True)
file = []
with os.scandir(directory) as listOfEntries:
    for entry in listOfEntries:
        if entry.is_file() and entry.name is not ".DS_Store":
            file.append(open(directory+entry.name).readlines())
    file = list(itertools.permutations(file))
    for b in range(0, len(file)):
        for i in itertools.product(*file[b]):
            test = '\n'.join(i).replace("\n", "")
            erg.stdin.writelines(test+'\n')

print(erg.communicate()[0])

这是我与沟通的输出:

Session..........: hashcat
Status...........: Cracked
Hash.Type........: SHA-512
Hash.Target......:          7ba4e9da57a7d3bd8b1b43c0b028a96d77721f6b33e3b85f0b2...298b56
Time.Started.....: Sat Feb 24 03:52:05 2018 (0 secs)
Time.Estimated...: Sat Feb 24 03:52:05 2018 (0 secs)
Guess.Base.......: Pipe
Speed.Dev.#2.....:   969.7 kH/s (0.13ms)
Recovered........: 1/1 (100.00%) Digests, 1/1 (100.00%) Salts
Progress.........: 384
Rejected.........: 0
Restore.Point....: 0
Candidates.#2....: telefon1telefon3telefon2 -> tasse2tasse3tasse1

这是我的输出,带有带有stdout.readline的for循环:

Session..........: hashcat
Status...........: Running
Hash.Type........: SHA-512
Hash.Target......:  7ba4e9da57a7d3bd8b1b43c0b028a96d77721f6b33e3b85f0b2...298b56
Time.Started.....: Sat Feb 24 04:14:30 2018 (10 secs)
Time.Estimated...: Sat Feb 24 04:14:40 2018 (0 secs)
Guess.Base.......: Pipe
Speed.Dev.#2.....:        0 H/s (0.00ms)
Recovered........: 0/1 (0.00%) Digests, 0/1 (0.00%) Salts
Progress.........: 0
Rejected.........: 0
Restore.Point....: 0
Candidates.#2....: [Copying]

如您所见,我得到一个输出,但是hashcat进程没有得到我的stdin流或不对其进行处理,我也不知道为什么。

如何用代码实现连续输出?

JohanL在评论中的回答是我的解决方案。 对此非常感谢。

您应该考虑使用线程,并在单独的线程中运行stdout和stdin,例如,在我的回答中:[post的链接] [stackoverflow.com/a/48777349/7738328 – JohanL]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM