How to generate a random password using Python and generate a random password using python
I wrote a program mainly used to check the empty and weak passwords of the MySQL database,
Three weak passwords are defined here:
1. Continuous numbers, such as 123456, are implemented in get_weak_num
2. Continuous letters, such as abcdef, are implemented in get_weak_character.
Of course, the numbers are all random.
3. random combination of numbers and letters. It is implemented in get_weak_num_character.
A list of password_exist is defined to save different passwords. If the newly generated password exists in the list, the connection to the MySQL database will not be performed, and the next cycle will be taken directly.
The details are as follows:
# Coding = utf8import random, string, MySQLdbdef get_num (): return random. randint (0, 9) def get_char (): return random. choice (tuple (string. lowercase) def choose_any (): return [str (get_num (), get_char ()] def get_weak_num (): weak_num = [] initial_num = get_num () for I in range (get_num (): weak_num.append (str (initial_num + I) if initial_num + I = 9: break; return weak_numdef limit (): weak_character = [] I Nitial_character = get_char () for I in range (get_num (): weak_character.append (chr (ord (initial_character) + I) if chr (ord (initial_character) + I) = 'Z': break return weak_characterdef get_weak_num_character (): return [random. choice (choose_any () for num in range (get_num ()] password_exist = [] for I in range (10000): choice = [get_weak_num (), get_weak_character (), get_weak_num_character ()] password = ''. join (Random. choice (choice) print "no" + str (I) + "password:" + password if password in password_exist: continue else: try: MySQLdb. connect ('2017. 168.244.145 ', 'root', password) print 'the password for MySQL is:' + password break failed T: continue password_exist.append (password) if I = 9999: print 'the password is not so weak ~'