Example of using Python to generate a random password-Python tutorial

Source: Internet
Author: User
This article describes how to use Python to generate a random password. for example, you can set a random password for users to modify in case of security issues, it is really convenient to use python to generate a random password for a friend who needs it. the beautiful string method and choice are just out of stock.
Make_password.py

### A few simple lines of code can be executed to generate a string that cannot be remembered ### $ python make_passwd.py DLrw9EiT Qs4Wm84q RQwl4L2L u9425lgww jHPtYdyU...

$ python make_passwd.py DLrw9EiT Qs4Wm84q RQwl4L2L u9g0LgwW jHPtYdyU ...

The code is as follows -- the comment is longer than the code

#! /Usr/bin/python # -- coding: UTF-8 -- # coding # Name: make_passwd # Author: LiuSha # Created: 28/12/2014 # Copyright: (c) WDZJ-SA 2014 # ------------------------------------------------------------------------------- from random import choice import stringdef Makepass (length = 8, chars = string. letters + string. digits): return ''. join ([choice (chars) for I in range (length)]) if _ name _ = '_ main _': for I in range (10 ): print Makepass () # The following example is basically the core of all the work of this small script. use the choice method of the random module to obtain the string generated by the string Module ##>> string. letters 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy' >>> string. digits '000000' >>> choice (string. digits) '4' >>> choice (string. letters) 'T' # For more information about generators, see: http://www.ipython.me/python/python-generator.html###! /Usr/bin/python # -- coding: UTF-8 -- # coding # Name: make_passwd # Author: LiuSha # Created: 28/12/2014 # Copyright: (c) WDZJ-SA 2014 # ------------------------------------------------------------------------------- from random import choice import stringdef Makepass (length = 8, chars = string. letters + string. digits): return ''. join ([choice (chars) for I in range (length)]) if _ name _ = '_ main _': for I in range (10 ): print Makepass () # The following example is basically the core of all the work of this small script. use the choice method of the random module to obtain the string generated by the string Module ##>> string. letters 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy' >>> string. digits '000000' >>> choice (string. digits) '4' >>> choice (string. letters) 'T' # For more information about generators, see: http://www.ipython.me/python/python-generator.html##


Generate some passwords that seem to be easy to remember (Qs4Wm84q seems to have no choice but to copy and paste the passwords. In other words, I used a similar password generated by shell to use ldap as the default password the year before, at that time, some employees in my company had backed up the password. now I think it's really amazing ~~~).

# This looks better than the above, but you need to provide a dictionary file # $ python make_dictpass.py 1 8 1 ipythosd $ python make_dictpass.py nahontchen chenyibfeo ipythoniue coreostche... $ python make_dictpass.py 1 8 1 ipythosd $ python make_dictpass.py nahontchen chenyibfeo ipythoniue coreostche...

The code is as follows:

#! /Usr/bin/python # -- coding: UTF-8 -- # coding # Name: make_dictpass # Author: LiuSha # Created: 28/12/2014 # Copyright: (c) WDZJ-SA 2014 # ------------------------------------------------------------------------------- import random import stringclass passwd (): data = open ('. /word.txt '). read (). lower () def renew (self, n, maxmem = 3): self. chars = [] for I in range (n): randspot = random. randrange (len (self. data) self. data = self. data [randspot:] + self. data [: randspot] where =-1 locate = ''. join (self. chars [-maxmem:]) while where <0 and locate: where = self. data. find (locate) locate = locate [1:] c = self. data [where + len (locate) + 1] if not c. islower (): c = random. choice (string. lowercase) self. chars. append (c) def _ str _ (self): return ''. join (self. chars) if _ name _ = '_ main _': import sys # if there is a parameter, you can define the number of times the password is generated, trace record # if len (sys. argv)> 1: dopass = int (sys. argv [1]) else: dopass = 8 if len (sys. argv)> 2: length = int (sys. argv [2]) else: length = 10 if len (sys. argv)> 3: memory = int (sys. argv [3]) else: memory = 3 onepass = passwd () for I in range (dopass): onepass. renew (length, memory) print onepass

# Dictionary File (which can be a combination of various words) # $ cat word.txt chenyi itchenyi python ipython coreos. me ipython. me

#! /Usr/bin/python # -- coding: UTF-8 -- # coding # Name: make_dictpass # Author: LiuSha # Created: 28/12/2014 # Copyright: (c) WDZJ-SA 2014 # ------------------------------------------------------------------------------- import random import stringclass passwd (): data = open ('. /word.txt '). read (). lower () def renew (self, n, maxmem = 3): self. chars = [] for I in range (n): randspot = random. randrange (len (self. data) self. data = self. data [randspot:] + self. data [: randspot] where =-1 locate = ''. join (self. chars [-maxmem:]) while where <0 and locate: where = self. data. find (locate) locate = locate [1:] c = self. data [where + len (locate) + 1] if not c. islower (): c = random. choice (string. lowercase) self. chars. append (c) def _ str _ (self): return ''. join (self. chars) if _ name _ = '_ main _': import sys # if there is a parameter, you can define the number of times the password is generated, trace record # if len (sys. argv)> 1: dopass = int (sys. argv [1]) else: dopass = 8 if len (sys. argv)> 2: length = int (sys. argv [2]) else: length = 10 if len (sys. argv)> 3: memory = int (sys. argv [3]) else: memory = 3 onepass = passwd () for I in range (dopass): onepass. renew (length, memory) print onepass

# Dictionary File (which can be a combination of various words) # $ cat word.txt chenyi itchenyi python ipython coreos. me ipython. me

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.