pyhton2 and python3 生成随机数字、字母、符号字典(用于撞库测试/验证码等)
本文介绍Python3中String模块ascii_letters和digits方法,其中ascii_letters是生成所有字母,从a-z和A-Z,digits是生成所有数字0-9.string.punctuation是所有标点'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
String模块中的常量:
string.digits:数字0~9
string.ascii_letters:所有字母(大小写)
string.lowercase:所有小写字母
string.printable:可打印字符的字符串
string.punctuation:所有标点
string.uppercase:所有大写字母
>>> import string >>> string.digits '0123456789' >>> string.letters 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' >>> string.lowercase 'abcdefghijklmnopqrstuvwxyz' >>> string.printable '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c' >>> string.punctuation '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~' >>> string.uppercase 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
#!/usr/bin/env python
# encoding: utf-8
#@author: jack
#@contact: 935650354@qq.com
#@site: https://www.cnblogs.com/jackzz
#@software: PyCharm
import threading
import queue
import random, string
class ThreadPool:
def __init__(self, n):
"""
构造一个指定数量线程的线程池.
:param n: 线程数量
"""
self.queue_obj = queue.Queue()
for i in range(n):
# 开启池中的线程对象
threading.Thread(target=self.worker, daemon=True).start()
def worker(self):
"""
线程对象, 写while True是为了能够一直去执行任务.
:return: None
"""
while True:
func = self.queue_obj.get()
func()
self.queue_obj.task_done()
def apply_async(self, func):
"""
向队列中传入的需要执行的函数对象.
:param func: 函数对象
:return: None
"""
self.queue_obj.put(func)
def join(self):
"""
等待队列中的内容被取完.
:return: None
"""
self.queue_obj.join()
def rand_str(num, length=8):
with open('pwd_dir.txt', 'w') as f:
for i in range(num):
chars = string.ascii_letters+string.digits+string.punctuation
s = [random.choice(chars) for i in range(length)]
s=set(s)
f.write('{0}\n'.format(''.join(s)))
f.close()
if __name__ == '__main__':
P = ThreadPool(100)#不怕内存溢出的小伙伴往大了设,一般100个满足日常所需了
P.apply_async(rand_str(200000000))#跑个2E个组合的字典
P.join()
python2 方法一、
#!/usr/bin/env python # -*- coding: utf-8 -*- import random import string #第一种方法 seed = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+=-" sa = [] for i in range(8): sa.append(random.choice(seed)) salt = ''.join(sa) print salt
方法二、
#!/usr/bin/env python # -*- coding: utf-8 -*- import random import string salt = ''.join(random.sample(string.ascii_letters + string.digits, 8)) print salt
逆风的方向更适合飞翔,不怕千万人阻挡,只怕自己投降!