最近開始學習PYTHON編程語言,詳細參照《python絕技運用Python成爲頂級***》。在學習過程第一章節中,編寫破解LINUX shadow文件時,想利用多線程加快破解速度。主機運行環境爲WINDOWS下的VM WORKSTATION上的一臺虛擬機,運行多線程代碼後並沒有任何速度上的提高,而且常常伴隨輸出混亂,不知因此然。故仍是利用多進程編寫了一個簡單的腳本文件,代碼以下:python
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:
passlinux
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 passwdlistapp
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()編程語言
運行效率比多線程要快不少
ide