locust 签名处理脚本
一、locust 处理签名加密脚本:
from locust import HttpUser, TaskSet, task
import os, queue, json
import random
import time
import hashlib
class MyTask(TaskSet):
@task
def ParamsTest(self):
self.headers = {
"Content-Type":"application/json"
}
self.data = {"phoneNum":"123434","optCode":"testfan","timestamp":"12112121212","sign":"fdsfdsaafsasfas"}
#随机手机号
str_start = random.choice(['135', '136', '138'])
str_end = ''.join(random.sample('0123456789', 8))
str_phone = str_start + str_end
self.data["phoneNum"] = str_phone
#随机时间
MyTime = int(time.time()*1000)
# 拼接手机号 + testfan + 时间
contact = f'{str_phone}testfan{MyTime}'
#对拼接后的字符串进行加密处理
sign_data = self.get_md5(contact)
self.data["phoneNum"] = str_phone
self.data["timestamp"] = MyTime
self.data["sign"] = sign_data
response = self.client.post("/pinter/com/userInfo",headers = self.headers ,data= json.dumps(self.data),name="SignTest")
print(response.text)
#定义MD5加密函数
def get_md5(self,data):
md5 = hashlib.md5()
md5.update(data.encode("utf-8"))
return md5.hexdigest()
class WebsiteUser(HttpUser):
task = tasks = [MyTask]
max_wait = 4000
min_wait = 6000
if __name__ == "__main__":
os.system("locust -f SignTest.py --host=http://localhost:8088")