前言
在之前的隨筆中,我們已經學過了如何使用使用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())
