python模块密码验证码生成
''' 密码生产 验证码生产 ''' import string import random print(string.ascii_letters) print(string.digits) print(string.digits+string.ascii_letters+string.punctuation)
'''
>>> print(string.ascii_letters)
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
>>> print(string.punctuation)
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
>>> print(string.digits)
0123456789
>>> print(string.digits+string.ascii_letters+string.punctuation)
0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
>>>
''' s=string.digits+string.ascii_letters+string.punctuation yzm = random.sample(s,6) print(yzm) msg = ''.join(random.sample(s,10)) print(msg) #洗牌打乱
#random() 方法返回随机生成的一个实数,它在[0,1)范围内。
#shuffle() 方法将序列的所有元素随机排序。
#洗牌打乱
data=list(range(10))
#print(data)
random.shuffle(data)
print(data)
import random
list = [20, 16, 10, 5];
random.shuffle(list)
print("随机排序列表 : ", list)
random.shuffle(list)
print("随机排序列表 : ", list)
[3, 7, 8, 6, 9, 1, 4, 0, 5, 2]
随机排序列表 : [16, 20, 10, 5]
随机排序列表 : [20, 10, 5, 16]
>>> d = list(range(10))
>>> d
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> random.shuffle(d)
>>> d
[5, 4, 9, 6, 3, 1, 0, 8, 2, 7]
>>> d
[5, 4, 9, 6, 3, 1, 0, 8, 2, 7]
>>>
密码生产:10台服务器,生产10个密码随机密码
def passwd_code():
ret=[]
num = string.digits
alf = string.ascii_letters
res = num + alf
s = random.sample(res,10)
ret = ''.join(random.sample(s, 10))
return ret
if __name__ == '__main__':
print('USER','PORT','PASSWORD')
for i in range(10):
print('root','22',passwd_code())
import string,random def passwd_code(): ret=[] num = string.digits alf = string.ascii_letters res = num + alf s = random.sample(res,10) ret = ''.join(random.sample(s, 10)) return ret if __name__ == '__main__': print('USER','PORT','PASSWORD') passwd_data = 'passwd_data.txt' with open(passwd_data, 'a') as f: for i in range(1,16): info = '192.168.1.%s root 22 ' %i info = info+passwd_code() + '\n' print(info) f.write(info)
优化后代码:
import string,random def passwd_code(): ret=[] num = string.digits alf = string.ascii_letters res = num + alf s = random.sample(res,10) ret = ''.join(random.sample(s, 10)) return ret if __name__ == '__main__': print('USER','PORT','PASSWORD') passwd_data = 'passwd_data.txt' strtmp = '' with open(passwd_data, 'a') as f: for i in range(1,16): info = '192.168.1.%s root 22 ' %i strtmp = strtmp+info+passwd_code() + '\n' print(strtmp) f.write(strtmp) #字符串最后一次写入