How to generate a random password in Python

Source: Internet
Author: User
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!

Related Article

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.