Home > Backend Development > Python Tutorial > How to generate random passwords in Python

How to generate random passwords in Python

高洛峰
Release: 2017-03-04 16:21:06
Original
1571 people have browsed it

This example shares the implementation process of generating random passwords in Python for your reference. The specific content is as follows

Written a program, mainly used to detect empty passwords and weak passwords in the MySQL database For passwords,

here, three types of weak passwords are defined:

1. Continuous numbers , such as 123456, implemented in get_weak_num

2. Continuous letters , such as abcdef, are implemented in get_weak_character

. Of course, the number is random.

3. Random combination of numbers and letters. Implemented in get_weak_num_character.

At the same time, 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 it will go directly to the next cycle.

The details are as follows:

#coding=utf8
import random,string,MySQLdb
def 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_num
def get_weak_character():
  weak_character=[]
  initial_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_character
def 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 "第"+str(i)+"次密码为:"+password
  if password in password_exist:
    continue
  else:
    try:
      MySQLdb.connect('192.168.244.145', 'root', password)
      print 'The password for MySQL is:'+password
      break
    except:
      continue
    password_exist.append(password)
if i == 9999:
  print 'The password is not so weak~'
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone's study.

For more related articles on how to generate random passwords in Python, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template