python随机生成个人信息

python随机生成个人信息

python随机生成个人信息

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import random
import sys, pymysql
import threading
'''
PyMySQL==0.9.3
'''
class PersonalInformation(): # 生成姓名 def Names_of_generated(self): list_Xing = [ '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '羿', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '宿', '', '怀', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '寿', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '广', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '万俟', '司马', '上官', '欧阳', '夏侯', '诸葛', '闻人', '乐正', '壤驷', '公良', '拓跋', '漆雕', '夹谷', '宰父', '东方', '赫连', '皇甫', '尉迟', '公羊', '澹台', '公冶', '宗政', '濮阳', '淳于', '单于', '太叔', '申屠', '公孙', '仲孙', '南宫', '南门', '呼延', '羊舌', '微生', '梁丘', '左丘', '东门', '西门', '东郭', '巫马', '公西', '谷梁', '轩辕', '令狐', '钟离', '宇文', '长孙', '慕容', '鲜于', '闾丘', '司徒', '司空', '亓官', '司寇', '颛孙', '端木', '子车', '百里', '段干' ] list_Ming = [ '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '广', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '' ] return random.choice(list_Xing) + random.choice(list_Ming) + random.choice(list_Ming) # 生成有效手机号码 def phoneNORandomGenerator(self): prelist = ["130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "147", "150", "151", "152", "153", "155", "156", "157", "158", "159", "186", "187", "188"] return random.choice(prelist) + "".join(random.choice("0123456789") for i in range(8)) def ageInfo(self): return random.randint(12, 60) def sexInfo(self): sex = random.choice(['', '']) return sex def getBirthday(self): year = random.randint(1960, 2000) month = random.randint(1, 12) if year % 4 == 0: if month in (1, 3, 5, 7, 8, 10, 12): day = random.randint(1, 31) elif month in (4, 6, 9, 11): day = random.randint(1, 30) else: day = random.randint(1, 29) else: if month in (1, 3, 5, 7, 8, 10, 12): day = random.randint(1, 31) elif month in (4, 6, 9, 11): day = random.randint(1, 30) else: day = random.randint(1, 28) # 小于10的月份前面加0 if month < 10: month = '0' + str(month) if day < 10: day = '0' + str(day) birthday = str(year) + str(month) + str(day) return birthday def idnum(self): province_id = [11, 12, 13, 14, 15, 21, 22, 23, 31, 32, 33, 34, 35, 36, 37, 41, 42, 43, 44, 45, 46, 50, 51, 52, 53, 54, 61, 62, 63, 65, 65, 81, 82, 83] id_num = '' id_num += str(random.choice(province_id))for i in range(4): ran_num = str(random.randint(0, 9)) id_num += ran_num b = self.getBirthday() id_num += b num = '' for i in range(2): num += str(random.randint(0, 9)) id_num += num s = self.sexInfo() if s == '': seventeen_num = random.randrange(1, 9, 2) else: seventeen_num = random.randrange(2, 9, 2) id_num += str(seventeen_num) eighteen_num = str(random.randint(1, 10)) if eighteen_num == '10': eighteen_num = 'X' id_num += eighteen_num return id_num # 生成信息,结果是一个字典 def PersonalInfo(): PersonalInfo = PersonalInformation() info = {} info['name'] = PersonalInfo.Names_of_generated() info['phone'] = PersonalInfo.phoneNORandomGenerator() info['emali'] = info['phone'] + random.choice( ['@163.com', '@qq.com', '@126.com', '@sina.com', '@sina.cn', '@soho.com', '@yeah.com']) info['age'] = PersonalInfo.ageInfo() info['sex'] = PersonalInfo.sexInfo() info['birthday'] = PersonalInfo.getBirthday() info['idnum'] = PersonalInfo.idnum() return info # print(PersonalInfo()) dblink = pymysql.connect( host="10.10.10.1", user="yong", password="yong", database="test", charset="utf8") # 创建一个锁就是通过threading.Lock()来实现 # 当多个线程同时执行lock.acquire()时,只有一个线程能成功地获取锁,然后继续执行代码,其他线程就继续等待直到获得锁为止。 # 获得锁的线程用完后一定要释放锁,否则那些苦苦等待锁的线程将永远等待下去,成为死线程。 # 所以我们用try...finally来确保锁一定会被释放。 # 插入数据 def install(db): cursor = db.cursor() sql = "insert `test`.`userinfo` (name,email,age,sex,idnumber,phone) values( %s,%s,%s,%s,%s,%s )" data = (PersonalInfo()['name'], PersonalInfo()['emali'], PersonalInfo()['age'], PersonalInfo()['sex'], PersonalInfo()['idnum'], PersonalInfo()['phone']) # print(data)lock.acquire() # 获取锁 try: cursor.execute(sql, data) db.commit() finally: # 确保锁一定会被释放。当在try块中抛出一个异常,立即执行finally块代码。 lock.release() # 释放锁 lock = threading.RLock() # 创建锁 threads = [] if __name__ == '__main__': # target=执行的函数,args=传给函数的值,range代表打开几个线程执行,变量i传给函数执行的函数的形参 for i in range(500): t = threading.Thread(target=install, args=(dblink,)) t.start() # 打开线程活动 # 等待至线程中止 for thr in threads: thr.join()

参考博客: https://www.cnblogs.com/lxmtx/p/11840221.html

02-04 21:53