1.1 session请求示例(有缓存作用,如果需要做接口关联,建议使用session请求
from Utils.local_config_utils import LocalConfig import requests import unittest import re class TestCases(unittest.TestCase): def setUp(self) -> None: self.url = LocalConfig.PHP_HOST self.session = requests.session() def tearDown(self) -> None: pass def test_case_regist(self): #请求头 header_info = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", "Accept-Encoding": "gzip, deflate", "Accept-Language": "zh-CN,zh;q=0.9", "Content-Type":"application/x-www-form-urlencoded" } #请求参数 get_token_param = {"m":"u", "c":"register"} token_response = self.session.get(url=self.url+‘/phpwind/index.php‘,params = get_token_param, ) info = token_response.content.decode(‘utf-8‘) #响应报文处理,将content返回的二进制 转换成‘utf-8格式‘ token = re.findall(‘name="csrf_token" value="(.+?)"/></form>‘,info )[0] #正则表达式提取需要的字符串 # print(info) print(token) #post #请求参数 params = { "m": "u", "c": "register", "a": "dorun" } #请求消息体 data = { "username": "liangyq10", "password": "123456", "repassword": "123456", "email": "1441404602@163.com", "csrf_token": token } #请求 reponse = self.session.post(url=self.url+‘/phpwind/index.php‘,params=params, data =data, headers=header_info ) reponse.content.decode(‘utf-8‘) if __name__ == ‘__main__‘: testsuite = unittest.TestSuite() testsuite.addTest(TestCases(‘test_case_regist‘)) runner = unittest.TextTestRunner() runner.run(testsuite)
* requests.get() 和 requests.post()与上述session请求语法一致
2.1 响应:
print("reponse.text====>",reponse.text) #返回响应报文 print("reponse.content====>",reponse.content) #返回响应报文 print("reponse.content.decode(‘utf-8‘)====>",reponse.content.decode(‘utf-8‘)) #返回响应报文 print("reponse.status_code====>", reponse.status_code) #状态码 print( "reponse.headers====>",reponse.headers) #响应头 print( "reponse.json()====>",reponse.json()) #以json格式化 print( "reponse.cookies====>",reponse.cookies) print( "reponse.raw====>", reponse.raw) print( "reponse.url====>", reponse.url) #返回请求url和参数 print( "reponse.apparent_encoding====>",reponse.apparent_encoding) # ascii print( "reponse.history====>",reponse.history) #重定向的历史请求 print( "reponse.raise_for_status()====>",reponse.raise_for_status()) #默认如果不是200就报异常
原文:https://www.cnblogs.com/joy-field/p/13299908.html