Python-簽名接口測試


前言

在之前的隨筆中,我們已經學過了如何使用使用JMeter和Postman實現sign簽名接口校驗的接口測試,今天我們來學習一下如何寫Python腳本實現簽名接口的接口測試。

簽名接口:

 

地址:http://localhost:8080/pinter/com/userInfo

 

參數為: {"phoneNum":"123434","optCode":"testfan","timestamp":"1211212","sign":"fdsfdsaafsasfas"} 其中,sign字段是按照特定算法進行加密后的數據

 

本接口的簽名算法為 sign=Md5(phoneNum+ optCode+ timestamp)

代碼如下:

 

import time
import random
import hashlib
import requests
import json
#1.生成5位隨機數
phone=random.randint(10000,99999)
#2.生成13位數字的時間戳
timeStamp=int(round(time.time()*1000))
print(timeStamp)
optCode="testfan"
#3.隨機數和時間戳拼接
t=str(phone+timeStamp)
#4.sign=隨機數phoneNum+optCode
sign=t+optCode
#5.實例化一個md5對象
md5=hashlib.md5()
#6.sign字段進行md5加密
md5.update(sign.encode("utf-8"))
print(md5.hexdigest())

def md5_sign():
   url ="http://localhost:8080/pinter/com/userInfo"
   header={"Content-Type":"application/json" }
   body={"phoneNum":phone,"optCode":"testfan","timestamp":timeStamp,"sign":md5.hexdigest()}
   respon = requests.post(url=url, headers=header,data=body)
   return respon.json()

if __name__ == '__main__':
      print(md5_sign())

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM