51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1993|回复: 2

Python做一个接口压测

[复制链接]

该用户从未签到

发表于 2019-3-26 11:14:11 | 显示全部楼层 |阅读模式
  1. #!/usr/bin/env python
  2. #coding=utf8

  3. import httplib, urllib, urllib2, json,random,datetime
  4. nowTimestyle=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  5. nowTime=datetime.datetime.now().strftime("%Y%m%d%H%M%S");#生成当前时间
  6. #print nowTime
  7. randomNum=random.randint(0,10000);#生成的随机整数n,其中0<=n<=100
  8. if randomNum<=1000:
  9.     randomNum=str(0)+str(randomNum);   #不足位用0补齐
  10. uniqueNum=str(nowTime)+str(randomNum);
  11. #print uniqueNum


  12. for i in range (0,10):
  13.     def http_post():
  14.         nowTimestyle=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  15.         nowTime=datetime.datetime.now().strftime("%Y%m%d%H%M%S");#生成当前时间
  16.         #print nowTime
  17.         randomNum=random.randint(0,1000);#生成的随机整数n,其中0<=n<=100
  18.         if randomNum<=100:
  19.             randomNum=str(0)+str(randomNum);
  20.         uniqueNum=str(nowTime)+str(randomNum);
  21.         serverurl = 'http://192.168.1.21/路径'
  22.         postData = {"extReservationNo"  :uniqueNum,
  23.             "memberId"          :"2017045000",
  24.             "reservationTime"   :nowTimestyle,
  25.             "reservationAmount" :"2000.00",
  26.             "productCode"       :"123456",
  27.             "saleChannel"       :"2554",
  28.             "userName"          :"A",
  29.             "certNo"            :"41111555",
  30.             "telNo"             :"151210111"}


  31.         params = json.dumps(postData,indent =4)             # 对数据进行JSON格式化编码
  32.         print(params)
  33.         request = urllib2.Request(serverurl, params)       # 生成页面请求的完整数据
  34.         request.add_header('Content-Type','application/json')
  35.         request.get_method = lambda:'POST'           # 设置HTTP的访问方式
  36.         response = urllib2.urlopen(request)       # 发送页面请求
  37.         return response.read()                    # 获取服务器返回的页面信息
  38.   
  39.   
  40.     resp = http_post()
  41.     print resp
复制代码


回复

使用道具 举报

该用户从未签到

发表于 2019-4-25 15:01:14 | 显示全部楼层
引用线程,进程的概念会不会更好,这样还可以做并发
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-3-29 14:57 , Processed in 0.064539 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表