您的位置:首页 > 编程语言 > Python开发

Python 生成MySQL 管理员随机复杂秘钥

2017-10-25 13:14 459 查看
Python生成随机秘钥:

#!/usr/bin/python
from random import choice as randomChoice

global passData
global password

passData = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
#'!', '@', '#', '$', '%', '^', '&', '*', '-', '_', '=',
# 这里注释是因为有$#!^等特殊字符,在shell下的mysql命令行里面不识别,而且写普通的shell脚本以及python会面临转义以及被截断的问题,导致无法登陆,所以下面保留了*/-/_/= 4种特殊符号
'*', '-', '_', '=',
'+', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ' ']

# 这个是调试的方法,界面控制台让你输入,如果再其它代码里面引用,需要自带参数
def newPass():
while True:
try:
passwdNum = int(raw_input("Enter max number of characters: "))
except TypeError:
print "Please enter a digit."
continue
break
get_password(passwdNum)

def get_password(number):
password = []
number = int(number)
count = 0
print "\nGenerating password..."
while count != number:
password.append(randomChoice(passData))
count += 1
x = 0
p = ''
while x != len(password):
p = p + str(password[x])
x += 1
#print "Password generated.",
#print "Here's your %s character password. Note that there may be spaces: %s" % (len(password), p)
return p

# 这里就是生成多少位秘钥,可以自己带参数
print get_password(16)
#newPass()


执行结果:

Generating password...
exgODezzhoPwL_NN

Generating password...
FxWbtKY=KKbczCPZ

Generating password...
NZIwnU8ij-w+Fxwu

Generating password...
BhbAgkBM0nparh8m
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python