• 从接口自动化测试框架设计到开发(七)--cookie处理


    模拟登陆获取cookie

    # -*- coding: utf-8 -*-
    # @Author: jiujiu
    # @Date:   2020-03-09 17:05:13
    # @Last Modified time: 2020-03-09 17:37:03
    import requests
    import json
    url = "http://10.1.30.118:3200/provider/user/user/login"
    data = {
        'loginName':'15519560000',
        'loginModel':'0',
        'password':'12345678',
        'loginType':'01',
        'timeStamp':'1582616038764'
        }
    res = requests.post(url,data).json()
    json.dumps(res,sort_keys=True,indent=10)
    print(res['data']['token'])

    运行结果

     简单的通过登录获取token,再通过添加token get到数据

    # -*- coding: utf-8 -*-
    # @Author: jiujiu
    # @Date:   2020-03-09 17:05:13
    # @Last Modified time: 2020-03-11 15:38:42
    import requests
    import json
    def login():
            url = "http://10.1.30.119:9005/user/login"
            data = {
                'loginName': '012345',
                'password': '123456',
                'loginType': '01',
                'loginModel': '0',
                'anti_cache': '1583909753278'
                }
            res = requests.post(url,data=data).json()
                # json.dumps(res,sort_keys=False,indent=1,ensure_ascii=False)
                # token = res['data']['token']
    #token = requests.utils.dict_from_cookiejar(token)
            # print(res)
            token = res['data']['token']
            # print(token)
            return token
    login()
    
    def get_data():
        print({'token':login()})
        url = "http://10.1.30.119:9005/query/selectDriverInfo?current=1&size=20&total=18&car_type=%E5%A4%A7%E5%9E%8B%E5%AE%A2%E8%BD%A6&sex=&time=&screenFiled=&deal_time_s=&deal_time_e=&anti_cache=1583909895350"
        header = {
        'token':login()
        }
        res = requests.get(url=url,headers=header)
        print(res.text)
    if __name__ == '__main__':
        get_data()

    运行结果:

  • 相关阅读:
    python:HTML转义
    Python 安装 MySQLdb
    C# 获取当前路径方法
    配置 vim Python IDE 开发环境
    python:HTML转义
    Python list去重
    源码探秘
    加号的作用
    多态练习
    在web service用Cache要导入System.Web.HttpRuntime
  • 原文地址:https://www.cnblogs.com/dydxw/p/12449958.html
Copyright © 2020-2023  润新知