121-cookie持久化4/用例库相关函数

参考大佬文章:121

目前逻辑:
首先这个demo函数是被数次执行的,每一个步骤接口,都会执行一次demo函数,每次之间,他们的数据共享,也就是我们的登陆态接口的返回值,是通过global 变成全局变量 来保存并使用的。但是我们为了避免多次调用登陆态接口,所以在每次去使用的时候先判断一下,是否存在公共变量,若存在就不用调用生成新的登陆态字段了,直接拿来用。但是如果没有,那么就只能去调用生成了

run_case

判断是否已经调用过代码:用的eval来判断这个所谓的login_res变量是否存在的

                try:
                    eval("login_res")  # 用eval来判断这个login_res变量是否存在的

首次去调用登录态接口生成login_res

url插入:

# 开始插入代码url
                if '?' not in url:
                    url += '?'
                    if type(login_res) == dict:
                        for i in login_res.keys():
                            url += i + '=' + login_res[i] + '&'
                else:
                    if type(login_res) == dict:
                        for i in login_res.keys():
                            url += '&' + i + '=' + login_res[i]

header插入:

# header插入
                if type(login_res) == dict:
                    header.update(login_res)

body插入:【注意变量名的修改&&添加判断if的位置】

            if api_body_method == 'none' or api_body_method == 'null':
                if type(login_res) == dict:
                    response = requests.request(api_method.upper(), url, headers=header, data={})  # 请求方式大写、URL、头部、数据
                else:
                    response = login_res.request(api_method.upper(), url, headers=header, data={})  # 请求方式大写、URL、头部、数据


            elif api_body_method == 'form-data':
                files = []
                payload = {}  # 字典
                for i in eval(api_body):
                    payload[i[0]] = i[1]
                if type(login_res) == dict:
                    for i in login_res.keys():
                        payload[i] = login_res[i]
                    response = requests.request(api_method.upper(), url, headers=header, data=payload, files=files)
                else:
                    response = login_res.request(api_method.upper(), url, headers=header, data=payload, files=files)

            elif api_body_method == 'x-www-form-urlencoded':
                header['Content-Type'] = 'application/x-www-form-urlencoded'
                payload = {}  # 字典
                for i in eval(api_body):
                    payload[i[0]] = i[1]
                if type(login_res) == dict:
                    for i in login_res.keys():
                        payload[i] = login_res[i]
                    response = requests.request(api_method.upper(), url, headers=header, data=payload)
                else:
                    response = login_res.request(api_method.upper(), url, headers=header, data=payload)

            elif api_body_method == 'GraphQL':
                header['Content-Type'] = 'application/json'
                query = api_body.split('*WQRF*')[0]
                graphql = api_body.split('*WQRF*')[1]
                try:
                    eval(graphql)
                except:
                    graphql = '{}'
                payload = '{"query":"%s","variables":%s}' % (query, graphql)
                if type(login_res) == dict:
                    response = requests.request(api_method.upper(), url, headers=header, data=payload)
                else:
                    response = login_res.request(api_method.upper(), url, headers=header, data=payload)


            else:  # 五个raw子选项
                if api_body_method == 'Text':
                    header['Content-Type'] = 'text/plain'
                if api_body_method == 'JavaScript':
                    header['Content-Type'] = 'text/plain'
                if api_body_method == 'Json':
                    api_body = json.loads(api_body)
                    for i in login_res.keys():
                        api_body[i] = login_res[i]
                    api_body = json.dumps(api_body)
                    header['Content-Type'] = 'text/plain'
                if api_body_method == 'Html':
                    header['Content-Type'] = 'text/plain'
                if api_body_method == 'Xml':
                    header['Content-Type'] = 'text/plain'
                if type(login_res) == dict:
                    response = requests.request(api_method.upper(), url, headers=header,
                                                data=api_body.encode('utf-8'))
                else:
                    response = login_res.request(api_method.upper(), url, headers=header,
                                                 data=api_body.encode('utf-8'))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值