python
import datetime
import json
import pymysql
import requests
import snowflake.client
from faker import Faker
#cmd启动snowflake服务:
#snowflake_start_server --address=localhost --port=8910 --dc=1 --worker=1
def create_testers():
# 创建一个中文Faker实例
fake = Faker('zh_CN')
url = "http://192.168.16.178:8081/v1/app/test/submit"
try:
# 创建一个数据库连接
conn = pymysql.connect(host='127.0.0.1',
port=3306,
user='root',
password='root',
database='yzpj'
)
print('连接mysql成功')
# 创建一个游标
cursor = conn.cursor()
# 创建一个sqL语句
sql = "INSERT INTO `t_tester`(`id`, `name`, `id_type`, `id_no`, `phone`, `gender`, `birthday`, `education`,`education_years`, `subjective_ses`, `family_monthly_earning`,`status`,`create_time`) values(%s, %s, %s, %s,%s, %s, %s, %s, %s, '%s', %s, %s, %s)"
# 循环生成用户数据 生成334次
for i in range(1, 101):
for j in range(10):
# 生成中文姓名
id = snowflake.client.get_guid()
name = fake.name()
id_type = 1
id_no = fake.ssn()
phone = fake.phone_number()
# 从身份证号中提取出生日期
birth_year = int(id_no[6:10])
birth_month = int(id_no[10:12])
birth_day = int(id_no[12:14])
birthday = datetime.date(birth_year, birth_month, birth_day)
# 提取性别
gender = int(id_no[-2])
if gender % 2 == 0:
gender = 2 # 女
else:
gender = 1 # 男
# 教育程度
education = fake.random_int(min=1, max=6, step=1)
education_years = fake.random_int(min=9, max=20, step=1)
# 社会地位
subjective_ses = fake.random_int(min=1, max=10, step=1)
# 家庭收入
family_monthly_earning = fake.random_int(min=1, max=8, step=1)
status = 1
create_time = fake.date_time_between(start_date='-10M', end_date='-9M')
# 执行sql语句
cursor.execute(sql, (id, name, id_type, id_no, phone, gender, birthday.strftime('%Y-%m-%d'), education,education_years, subjective_ses, family_monthly_earning, status, create_time.strftime('%Y-%m-%d %H:%M:%S')))
# 提交事务
conn.commit()
answer = {
"Q1": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q2": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q3": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q4": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q5": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q6": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q7": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q8": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q9": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q10": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q11": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q12": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q13": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q14": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q15": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q16": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q17": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q18": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q19": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q20": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q21": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q22": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q23": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q24": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q25": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q26": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q27": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q28": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q29": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q30": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q31": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q32": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q33": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q34": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q35": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q36": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q37": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q38": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q39": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q40": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q41": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q42": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q43": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q44": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q45": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q46": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q47": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q48": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q49": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q50": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q51": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q52": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q53": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q54": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q55": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q56": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q57": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q58": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"Q59": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"CH1": fake.random_element(elements=('A', 'B', 'C', 'D', 'E')),
"CH2": fake.random_element(elements=('A', 'B', 'C', 'D', 'E')),
"CH3": fake.random_element(elements=('A', 'B', 'C', 'D', 'E')),
"CH4": fake.random_element(elements=('A', 'B', 'C', 'D', 'E')),
"CH5": fake.random_element(elements=('A', 'B', 'C', 'D', 'E')),
"CH6": fake.random_element(elements=('A', 'B', 'C', 'D', 'E')),
"CH7": fake.random_element(elements=('A', 'B', 'C', 'D', 'E')),
"CH8": fake.random_element(elements=('A', 'B', 'C', 'D', 'E')),
"CH9": fake.random_element(elements=('A', 'B', 'C', 'D', 'E')),
"CH10": fake.random_element(elements=('A', 'B', 'C', 'D', 'E')),
"DC1": fake.random_element(elements=('A', 'B', 'C')),
"DC2": fake.random_element(elements=('A', 'B', 'C')),
"DC3": fake.random_element(elements=('A', 'B', 'C')),
"DC4": fake.random_element(elements=('A', 'B', 'C')),
"DC5": fake.random_element(elements=('A', 'B', 'C', 'D')),
"DC6": fake.random_element(elements=('A', 'B', 'C')),
"DC7": fake.random_element(elements=('A', 'B', 'C')),
"DC8": fake.random_element(elements=('A', 'B', 'C')),
"DC9": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K')),
"DC10": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"DC11": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"DC12": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"DC13": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G')),
"DC14": fake.random_element(elements=('A', 'B', 'C', 'D', 'E', 'F', 'G'))
}
json_answer = json.dumps(answer)
# 提交测评结果
data = {
"userId": str(id_no),
"testType": "1101",
"testTime": create_time.strftime('%Y-%m-%d %H:%M:%S'),
"userAnswer": json_answer,
"totalTime": str(fake.random_int(min=90, max=300, step=5)),
"stage": "1",
"status": "1"
}
json_data = json.dumps(data)
data_post(url, json_data)
# 提交事务
# conn.commit()
print('{}条数据插入成功'.format(i*10))
# 关闭游标
cursor.close()
# 关闭连接
conn.close()
print('关闭数据库连接')
except Exception as e:
print(e)
def data_post(url, data):
headers = {"content-type": "application/json;charset=utf-8"}
try:
response = requests.post(url=url, headers=headers, data=data)
if response.status_code == 200:
print('请求成功')
else:
raise Exception('未正常响应请求')
return data
except requests.exceptions.RequestException as e:
print(e)
if __name__ == '__main__':
create_testers()
前提必须先安装雪花服务
bash
安装Faker库
pip install faker
安装最新版PyMySQL
pip install pymysql
安装雪花算法库
pip install pysnowflake
安装requests库
pip install requests
cmd启动snowflake服务:
snowflake_start_server --address=localhost --port=8910 --dc=1 --worker=1