Use Python to generate a random password. Use python to generate a random password.

Source: Internet
Author: User

Use Python to generate a random password. Use python to generate a random password.

It is really convenient to use python to generate a random password. 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 ''. jo In ([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' # related generators can be referred to: 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

Articles you may be interested in:
  • How to generate random passwords or strings in python
  • Generate a random password using Python
  • Python3: how to generate a random password
  • Example of a random password dictionary Generator Implemented in python

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.