[英]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.