This article describes in detail how to generate a random password using Python. interested partners can refer to the standard instance to share with you the implementation process of generating a random password using Python, for your reference, the specific content is as follows:
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 numbersFor example, 123456 is implemented in get_weak_num.
2. consecutive lettersFor example, abcdef is 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 ~'
The above is all the content of this article, hoping to help you learn.
For more articles about how to generate a random password in Python, refer to the PHP Chinese website!