Recently began to learn the Python programming language, detailed reference to the Python stunt using Python to become the top hacker. In the first chapter of the learning process, when you write a hack Linux shadow file, you want to use multithreading to speed up the crack. The host running environment is a virtual machine on VM Workstation under Windows, running multithreaded code without any speed increase, and often accompanied by output confusion, do not know why. Therefore, the use of multiple processes to write a simple script file, the code is as follows:
Import Crypt
Import multiprocessing
Import time
Import Sys
def check (passwd):
Cryptword=crypt.crypt (Passwd.strip (' \ n '), Salt.strip (' \ n '))
If Cryptword==linuxword:
Print "Password is:" +passwd
Print "Harked,time now is:" +currtime ()
Else
Pass
def worker (Q):
While not Q.empty ():
passwd =q.get ()
Try
Check (passwd)
Finally
Q.task_done ()
Def currtime ():
Return Time.ctime ()
def txttoarray (Desfilepath):
Desdic=open (Desfilepath, ' R ')
Passwdlist=[]
For line in Desdic.readlines ():
Passwdlist.append (line)
Return passwdlist
If __name__== ' __main__ ':
Global Salt,linuxword
Print "Start time is:" +currtime ()
Q=multiprocessing. Joinablequeue ()
Linuxword= "$6$9CFIODLW$C34LCX4EYVCD9KYER3KQLCM6O8Q7URYUROT/K0WVSNNQXTGBPJGDRGYMJY4GVMNYGLUJC1J5WVEKRM/JXSQ.I"
salt= "$6$9cfiodl$"
Desfilepath= "/script/1/1.15/password.txt"
Passwdlist=txttoarray (Desfilepath)
Map (q.put,passwdlist)
Jobs=[multiprocessing. Process (target=worker,args= (q,)) for I in Xrange (10)]
Map (Lambda X:x.start (), jobs)
Q.join ()
Print "End time is:" +currtime ()
Runs much faster than multithreading
Python Multi-process programming