Python version 5

PythonBranch
Matthieu DUVAL 8 years ago
parent 732477d016
commit e06f39baae

@ -0,0 +1,88 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import multiprocessing
import sys
import time
import locale
from ProcessBruteForce import *
nbProcess = 4
verbose = False
class App():
def __init__(self):
## La liste contenant nos object Processe
self.processes = list()
self.queue = multiprocessing.Queue()
self.infoQueue = multiprocessing.Queue()
self.found = False
self.appTime = time.time()
global nbProcess
global verbose
for i in range(0,nbProcess):
p=ProcessBrurteForce(self.queue, i,nbProcess , "azerty","",6,verbose)
self.processes.append(p)
def run(self):
for proc in self.processes:
proc.start()
procAlive=True
totalMotParSecondes =0
while procAlive:
totalMotParSecondes =0
procAlive =False
for proc in self.processes:
#proc.join()
resultat = self.queue.get()
if resultat[0]== 0 :
if resultat[1] == True :
#print("RESULT: %s" % resultat[2])
self.displayResult(resultat[2])
for proc in self.processes:
proc.terminate()
sys.exit()
elif resultat[0] == 1:
WordsSeconds = round(resultat[1]/resultat[2])
totalMotParSecondes = totalMotParSecondes + WordsSeconds
locale.setlocale(locale.LC_ALL, 'french_france')
WordsSeconds = locale.format('%d', WordsSeconds, grouping=True)
print(resultat[4]+" : " + '{:>30}'.format(WordsSeconds) + " Mots/s \tMot actuel : " + resultat[3])
else:
print("/!\ useCase" + resultat[0] +"non traité")
#on verifie qu'il y a encore des process en cours (il n'y ap aps de do while en python
if proc.is_alive():
procAlive =True
if verbose:
locale.setlocale(locale.LC_ALL, 'french_france')
totalMotParSecondes = locale.format('%d', totalMotParSecondes, grouping=True)
print("Mots Par Secondes : " +'{:>30}'.format(totalMotParSecondes)+ " Mots/s")
return None
def displayResult(self,passTrouve):
if(time.time() - self.appTime) > 60 :
seconds = time.time() - self.appTime
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
d ,h = divmod(h, 24)
if d != 0:
print("Found ! : " + passTrouve +" en " + "%d:%02d:%02d:%02d" % (d,h, m, s))
elif h != 0 :
print("Found ! : " + passTrouve +" en " + "%d:%02d:%02d" % (h, m, s))
else:
print("Found ! : " + passTrouve +" en " + "%d:%02d" % (m, s))
else:
print("Found ! : " + passTrouve +" en " + str(time.time() - self.appTime) + " secondes")
#si le fichier est lancé seul
if __name__ == '__main__' :
application = App()
application.run()
sys.exit()

@ -1,83 +1,11 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import multiprocessing
import sys
import time
import locale
tabChar = list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ")
nbProcess = 4
verbose = False
class App():
def __init__(self):
## La liste contenant nos object Processe
self.processes = list()
self.queue = multiprocessing.Queue()
self.infoQueue = multiprocessing.Queue()
self.found = False
self.appTime = time.time()
global nbProcess
global verbose
for i in range(0,nbProcess):
p=ProcessBrurteForce(self.queue, i,nbProcess , "totor","",4,verbose)
self.processes.append(p)
def run(self):
for proc in self.processes:
proc.start()
procAlive=True
totalMotParSecondes =0
while procAlive:
totalMotParSecondes =0
procAlive =False
for proc in self.processes:
#proc.join()
resultat = self.queue.get()
if resultat[0]== 0 :
if resultat[1] == True :
#print("RESULT: %s" % resultat[2])
self.displayResult(resultat[2])
for proc in self.processes:
proc.terminate()
sys.exit()
elif resultat[0] == 1:
WordsSeconds = round(resultat[1]/resultat[2])
totalMotParSecondes = totalMotParSecondes + WordsSeconds
locale.setlocale(locale.LC_ALL, 'french_france')
WordsSeconds = locale.format('%d', WordsSeconds, grouping=True)
print(resultat[4]+" : " + '{:>30}'.format(WordsSeconds) + " Mots/s \tMot actuel : " + resultat[3])
else:
print("/!\ useCase" + resultat[0] +"non traité")
#on verifie qu'il y a encore des process en cours (il n'y ap aps de do while en python
if proc.is_alive():
procAlive =True
if verbose:
locale.setlocale(locale.LC_ALL, 'french_france')
totalMotParSecondes = locale.format('%d', totalMotParSecondes, grouping=True)
print("Mots Par Secondes : " +'{:>30}'.format(totalMotParSecondes)+ " Mots/s")
return None
def displayResult(self,passTrouve):
if(time.time() - self.appTime) > 60 :
seconds = time.time() - self.appTime
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
d ,h = divmod(h, 24)
if d != 0:
print("Found ! : " + passTrouve +" en " + "%d:%02d:%02d:%02d" % (d,h, m, s))
elif h != 0 :
print("Found ! : " + passTrouve +" en " + "%d:%02d:%02d" % (h, m, s))
else:
print("Found ! : " + passTrouve +" en " + "%d:%02d" % (m, s))
else:
print("Found ! : " + passTrouve +" en " + str(time.time() - self.appTime) + " secondes")
class ProcessBrurteForce(multiprocessing.Process):
def __init__(self,queue, idx, nbInstence,password, clef, limite, verbose):
@ -113,17 +41,13 @@ class ProcessBrurteForce(multiprocessing.Process):
#print(multiprocessing.current_process().name + "\tStart")
found , passTrouve = self.checkChar(0)
if not found:
while not found and len(self.tabPass) < self.limite:
self.tabPass = self.tabPass + list('a')
found,passTrouve =self.recursiveFunction(len(self.tabPass)-1)
if found == True :
passTrouve = "".join(self.tabPass)
if not found :
indexMin = round((len(tabChar)/self.nbInstence)*self.idx)
indexMax = round(indexMin + (len(tabChar)/self.nbInstence))
self.tabPass = self.tabPass + list(tabChar[indexMin])
#print(multiprocessing.current_process().name + "\tRange : " + str(indexMin) + " - " + str(indexMax))
if( self.idx == self.nbInstence - 1 ):
indexMax = len(tabChar)
for i in range(indexMin,indexMax):
self.tabPass[len(self.tabPass)-1] = tabChar[i]
found,passTrouve =self.recursiveFunction(len(self.tabPass)-2)
@ -168,7 +92,5 @@ class ProcessBrurteForce(multiprocessing.Process):
#si le fichier est lancé seul
if __name__ == '__main__' :
application = App()
application.run()
if __name__ == '__main__' :
sys.exit()
Loading…
Cancel
Save