1. 程式人生 > >python接口自動化測試二十:函數寫接口測試

python接口自動化測試二十:函數寫接口測試

urlencode on() AD http upd htm pda TP CA

# coding:utf-8
import requests
import re
from bs4 import BeautifulSoup

# s = requests.session() # 全局的s

def get_token(s):
‘‘‘
fuction: 獲取token
args: s 參數 -》s = requests.session()
:return anti_token ->{‘X-Anit-Forge-Token‘: ‘xx‘, ‘X-Anit-Forge-Code‘: ‘38515842‘}
‘‘‘
# 局部的s沒定義,從外部傳入s
url = ‘https://passport.lagou.com/login/login.html‘
h1 = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0"
}
r1 = s.get(url, headers=h1, verify=False)
# print(r1.text)

soup = BeautifulSoup(r1.content, "html.parser", from_encoding=‘utf-8‘)
tokenCode = {}
try:
t = soup.find_all(‘script‘)[1].get_text()
print(t)
tokenCode[‘X_Anti_Forge_Token‘] = re.findall(r"Token = ‘(.+?)‘", t)[0]
tokenCode[‘X_Anti_Forge_Code‘] = re.findall(r"Code = ‘(.+?)‘", t)[0]
return tokenCode
except:
print("獲取token和code失敗")
tokenCode[‘X_Anti_Forge_Token‘] = ""
tokenCode[‘X_Anti_Forge_Code‘] = ""
return tokenCode


def login_lgw(s, anti_token, user, psw):

url2 = ‘https://passport.lagou.com/login/login.json‘
h2 = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"X-Requested-With": "XMLHttpRequest",
"X-Anit-Forge-Token": anti_token[‘X_Anti_Forge_Token‘] ,
"X-Anit-Forge-Code": anti_token[‘X_Anti_Forge_Code‘],
"Referer": "https://passport.lagou.com/login/login.html"
}
body ={
"isValidate": "true",
"username": user,
"password": psw,
"request_form_verifyCode": "",
"submit": ""
}
print(s.headers) # s的頭部

# 更新s的頭部
s.headers.update(h2)
print(s.headers)
r2 = s.post(url2, data=body, verify=False)
print(r2.text)
return r2.json()

if __name__ == "__main__":
# 自測的內容
s = requests.session()
token = get_token(s)
print(token)
user = "1232"
psw = "322222"
login_lgw(s, token, user, psw)


技術分享圖片

技術分享圖片

python接口自動化測試二十:函數寫接口測試