Python做一个接口压测

来源:互联网 发布:双11淘宝能便宜多少 编辑:程序博客网 时间:2024/06/07 03:36
#!/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/trade/order/reservation'
        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