Python Multi-process programming

Source: Internet
Author: User
Tags crypt

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.