requests模块测试依赖登录的接口

 

import requests
import json
# s = requests.Session()
class byms:
    def __int__(self):
        self.session = requests.session()
    def login_byms(self):
        url = "http://127.0.0.1/api/mgr/signin"
        params  = {
            'username': 'byhy',
            'password': '88888888'
        }
        r = self.session.post(url,data = params)
        return r
    def searchCustomer(self,pagesize,pagenum,keywords):
        url = "http://127.0.0.1/api/mgr/customers"
        params = {
            'action':'list_customer', # 固定传参
            'pagesize':pagesize,
            'pagenum':pagenum,
            'keywords':keywords
        }
        r = self.session.get(url= url,params = params)
        return r

if __name__ == "__main__":
    object = byms()
    object.__int__()
    object.login_byms()
    result = object.searchCustomer(50,1,'')
    s = result.json() #以json格式显示返回值
    jsonFormat = json.dumps(s,indent=4,ensure_ascii=False) #格式化json返回值,纯粹是为了美化结果
    print(jsonFormat)

结果如下:

{
    "ret": 0,
    "retlist": [
        {
            "id": 183,
            "name": "渭城朝雨医院",
            "phonenumber": "0359",
            "address": "唐都"
        },
        {
            "id": 182,
            "name": "海南省人民医院",
            "phonenumber": "351202012",
            "address": "海南省人民路1号"
        },
        {
            "id": 181,
            "name": "北京市人民医院",
            "phonenumber": "351202011",
            "address": "北京市人民路1号"
        },
        {
            "id": 180,
            "name": "重庆市人民医院",
            "phonenumber": "351202010",
            "address": "重庆市人民路1号"
        },
        {
            "id": 179,
            "name": "上海市人民医院",
            "phonenumber": "351202009",
            "address": "上海市人民路1号"
        },
        {
            "id": 178,
            "name": "河南省人民医院",
            "phonenumber": "351202008",
            "address": "河南省人民路1号"
        },
        {
            "id": 177,
            "name": "河北省人民医院",
            "phonenumber": "351202007",
            "address": "河北省人民路1号"
        },
        {
            "id": 176,
            "name": "浙江省人民医院",
            "phonenumber": "351202006",
            "address": "浙江省人民路1号"
        },
        {
            "id": 175,
            "name": "陕西省人民医院",
            "phonenumber": "351202005",
            "address": "陕西省人民路1号"
        },
        {
            "id": 174,
            "name": "四川省人民医院",
            "phonenumber": "351202004",
            "address": "四川省人民路1号"
        },
        {
            "id": 173,
            "name": "江西省人民医院",
            "phonenumber": "351202003",
            "address": "江西省人民路1号"
        },
        {
            "id": 172,
            "name": "山东省人民医院",
            "phonenumber": "351202002",
            "address": "山东省人民路1号"
        },
        {
            "id": 171,
            "name": "山西省人民医院",
            "phonenumber": "351202001",
            "address": "山西省人民路1号"
        },
        {
            "id": 31,
            "name": "南京中医院3",
            "phonenumber": "2551867853",
            "address": "江苏省-南京市-秦淮区-汉中路-503"
        },
        {
            "id": 30,
            "name": "南京中医院2",
            "phonenumber": "2551867852",
            "address": "江苏省-南京市-秦淮区-汉中路-502"
        },
        {
            "id": 29,
            "name": "南京中医院1",
            "phonenumber": "2551867851",
            "address": "江苏省-南京市-秦淮区-汉中路-501"
        }
    ],
    "total": 16
}

 

posted @ 2021-07-03 21:39  Avicii_2018  阅读(86)  评论(0编辑  收藏  举报