Python做一个接口压测
#!/usr/bin/env python#coding=utf8
import httplib, urllib, urllib2, json,random,datetime
nowTimestyle=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
nowTime=datetime.datetime.now().strftime("%Y%m%d%H%M%S");#生成当前时间
#print nowTime
randomNum=random.randint(0,10000);#生成的随机整数n,其中0<=n<=100
if randomNum<=1000:
randomNum=str(0)+str(randomNum); #不足位用0补齐
uniqueNum=str(nowTime)+str(randomNum);
#print uniqueNum
for i in range (0,10):
def http_post():
nowTimestyle=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
nowTime=datetime.datetime.now().strftime("%Y%m%d%H%M%S");#生成当前时间
#print nowTime
randomNum=random.randint(0,1000);#生成的随机整数n,其中0<=n<=100
if randomNum<=100:
randomNum=str(0)+str(randomNum);
uniqueNum=str(nowTime)+str(randomNum);
serverurl = 'http://192.168.1.21/路径'
postData = {"extReservationNo":uniqueNum,
"memberId" :"2017045000",
"reservationTime" :nowTimestyle,
"reservationAmount" :"2000.00",
"productCode" :"123456",
"saleChannel" :"2554",
"userName" :"A",
"certNo" :"41111555",
"telNo" :"151210111"}
params = json.dumps(postData,indent =4) # 对数据进行JSON格式化编码
print(params)
request = urllib2.Request(serverurl, params) # 生成页面请求的完整数据
request.add_header('Content-Type','application/json')
request.get_method = lambda:'POST' # 设置HTTP的访问方式
response = urllib2.urlopen(request) # 发送页面请求
return response.read() # 获取服务器返回的页面信息
resp = http_post()
print resp
感谢分享 引用线程,进程的概念会不会更好,这样还可以做并发
页:
[1]