先说session吧:众所周知http请求是无状态的,需要靠session来维持会话,而session与cookie又紧密联系通过存储在cookie里面的sessionid;接下来讲如何利用这一机制发送请求
"""pt:1session保持发送登陆后的接口请求"""
# coding=utf-8
import json
import requests
import unittest
class Test(unittest.TestCase):
@classmethod
def setUpClass(cls):
data = {"userAccount": "admin", "loginPwd": "123456"}
global session
s = requests.session()
s.post(url="http://192.168.229.128:8080/cms/manage/loginJump.do", data=data)
session =s
@classmethod
def tearDownClass(cls):
pass
def test_session(self):
datas = {'userName': 'zhangsan', 'userEmail': '1932390299@qq.com',
'userMobile': '18871027706', 'userSex': '1','userAccount': 'dev', 'confirmPwd': '123456', 'loginPwd': '123456', 'id': ''}
conn = session.post(url="http://192.168.229.128:8080/cms/manage/saveSysUser.do", data=datas)
print conn.content
if __name__ == "__main__":
unittest.main()
结果如下:
C:\Python27\python.exe "D:\Pycharm\PyCharm 5.0.4\helpers\pycharm\utrunner.py" C:\Users\Administrator\PycharmProjects\py3project\interface\Paramters\toy.py true
Testing started at 20:38 ...
{"code":"400","msg":"保存用户信息失败,登录帐号已存在!","model":{}}
到这里有人会说学过类我不想用全局,不喜欢或者怕全局乱用影响脚本
没错写过类就用类啊那就用类啊来啊互相伤害啊
"""pt2:用类传session"""":
# coding=utf-8
import json
import requests
import unittest
class Test(unittest.TestCase):
@classmethod
def setUpClass(cls):
data = {"userAccount": "admin", "loginPwd": "123456"}
s = requests.session()
s.post(url="http://192.168.229.128:8080/cms/manage/loginJump.do", data=data)
cls.session = s
@classmethod
def tearDownClass(cls):
pass
def test_session(self):
datas = {'userName': 'zhangsan', 'userEmail': '1932390299@qq.com',
'userMobile': '18871027706', 'userSex': '1','userAccount': 'dev', 'confirmPwd': '123456', 'loginPwd': '123456', 'id': ''}
"""在哪里,在哪里见过你"""
conn = self.session.post(url="http://192.168.229.128:8080/cms/manage/saveSysUser.do", data=datas)
print conn.content
if __name__ == "__main__":
unittest.main()
"""pt3:用cookie请求接口"""":
import requests
import json
import unittest
class Interface(unittest.TestCase):
@classmethod
def setUpClass(cls):
req = requests.post(url="http://192.168.229.128:8080/cms/manage/loginJump.do",
data={"userAccount": "admin", "loginPwd": "123456"})
global cookie
cookie = req.cookies.get_dict()
@classmethod
def tearDownClass(cls):
pass
def test_login(self):
u"登录平台接口"
req = requests.post(url="http://192.168.229.128:8080/cms/manage/loginJump.do",
data={"userAccount": "admin", "loginPwd": "123456"})
dicts = json.loads(req.content)
try:
self.assertEqual(dicts['msg'], u"登录成功!", "status:200")
except AssertionError as e:
print(e)
else:
results = req.json()
print(json.dumps(results, sort_keys=True, indent=2, separators=(',', ': '), encoding="UTF-8", ensure_ascii=False))
def test_cookie_request(self):
u"保存用户接口"
datas = {'userName': 'zhangsan', 'userEmail': '1932390299@qq.com',
'userMobile': '18871027706', 'userSex': '1','userAccount': 'dev', 'confirmPwd': '123456', 'loginPwd': '123456', 'id': ''}
conn = requests.post(url="http://192.168.229.128:8080/cms/manage/saveSysUser.do", data=datas, cookies=cookie)
result = conn.json()
print(json.dumps(result, encoding="UTF-8", ensure_ascii=False, sort_keys=True, indent=2, separators=(',', ': ')))
#
# if __name__ == "__main__":
# unittest.main()
""""pt4:补充点小故事:""""
解决下大家的接口返回乱码问题:
通常我们写接口
data = {"userAccount": "admin", "loginPwd": "123456"}
response =requests.post(url='xxx', data =json.dumps(data),header ={"xx": "xxx"})
或者response =requests.post(url='xxx', json=data ,header ={"xx": "xxx"})
都是处理json类型请求格式乱码很好的解决办法
还可以这样解决见我另一篇文章: https://blog.csdn.net/chen498858336/article/details/83795742
利用session请求和cookie请求,token比较特殊不同于前面二者后面补上在这边文章末尾
最新推荐文章于 2024-07-14 15:52:08 发布