Python generates a random password with special characters, and python special characters

Source: Internet
Author: User

Python generates a random password with special characters, and python special characters

In daily O & M, if user management is involved, it will certainly be used to set a password for the user. In fact, I usually think it is okay to set a password, however, it is also very painful to let you knock on a random password with 12 characters with special characters. If you want to knock on 10 such random passwords, I guess people are crazy, so this is not suitable for people, but the machine is the best. Today we will learn a python script to generate the password we need, you can specify the password length, number, and number of characters at will. Let's look at the Code:

#!/usr/bin/env pythonimport stringfrom itertools import chainfrom random import choice, sampledef mkpasswd(length=12, digits=4, upper=3, lower=3):lowercase = string.lowercaseuppercase = string.uppercasesalt = '!@#$%^&*()><?'password = list(chain((choice(uppercase) for _ in range(upper)),(choice(lowercase) for _ in range(lower)),(choice(string.digits) for _ in range(digits)),(choice(salt) for _ in range((length - digits - upper - lower)))))return "".join(sample(password, len(password)))if __name__ == '__main__':print mkpasswd()#!/usr/bin/env pythonimport stringfrom itertools import chainfrom random import choice, sampledef mkpasswd(length=12, digits=4, upper=3, lower=3):lowercase = string.lowercaseuppercase = string.uppercasesalt = '!@#$%^&*()><?'password = list(chain((choice(uppercase) for _ in range(upper)),(choice(lowercase) for _ in range(lower)),(choice(string.digits) for _ in range(digits)),(choice(salt) for _ in range((length - digits - upper - lower)))))return "".join(sample(password, len(password)))if __name__ == '__main__':print mkpasswd()

The script is explained one by one. The required modules are imported in the first few lines, and then a mkpasswd function is defined. The function parameters can set the password length, number, upper-case and lower-case characters, next is the topic of the function:

Use the attributes of the string module to generate a case-sensitive string. salt is a special character set (which can be added by yourself). We will focus on password generation. It is first a list because list () is called () the function of the chain () function is to connect a group of iterator objects. The chain () contains three Generator expressions, each of which calls the for loop, then randomly select the specified number of characters. You may see a _ (underline) in the for loop. What is the role of this underline? I don't want to sell it out. In fact, it does not play any role. I understand it as a variable that I cannot use. This method can be used in the future. For example, you cannot use this variable during loop, it can be expressed by _ (underline.

The final result of the function is the string connected by return. The sample () function extracts the specified number of samples from a random sample. Two parameters are required, one is the sample, and the other is the number of samples, the process is random. In fact, this function is not fully functional in this script, because the generated password is 12 bits, and then 12 bits are taken out, there is no work to retrieve small samples from the big sample, but the random function is enabled, so the generated passwords are all random characters.

We will introduce you to the random generation of passwords with special characters in Python. I hope this will help you!

Articles you may be interested in:
  • How to generate random passwords or strings in python

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.