绕过cookies进行登录并封装请求方法

之前写了一篇使用session跨请求保持会话的帖子,这次在它的基础上对请求方法简单封装一下,可以达到复用的效果

1.先定义登录方法

在登录方法中利用session跨请求保持会话,并返回session,login_ApprExclusive.py代码如下:

# coding:utf-8


import requests


class Login:
    """定义登录方法"""

    @staticmethod
    def test_login():
        s = requests.session()
        jar = requests.cookies.RequestsCookieJar()  # 创建一个Cookie Jar对象
        jar.set('49BAC005-7D5B-4231-8CEA-16939BEACD67', 'cktest001')  # 向Cookie Jar对象中添加cookie值,这是登录的cookies
        jar.set('JSESSIONID', 'ED2D58E3B438CC7597CB52CD31489914')
        jar.set('JSESSIONIDSSO', 'D16E59009557B82F5D5885DC4DAA0179')
        s.cookies.update(jar)  # 把cookies追加到Session中
        return s  # 返回携带cookies的session

2.定义请求方法

把get和post请求方法进行封装,注意这里不是用requests来发送请求了,而是用上一步登录方法中返回的session来发送,这样就达到了跳过验证码登录的目的(使用登录后的session会话发送请求)
Run_Method.py的代码如下:

# coding:utf-8


import json
from case.login.login_ApprExclusive import Login


class RunMain:

    def __init__(self):
        self.s = Login()   # 获取登录方法中的session

    def send_post(self, url, header, data):
        r = self.s.test_login().post(url=url, headers=header, data=data, verify=False)  # 使用session发送请求
        result = r.json()
        return json.dumps(result, indent=2, sort_keys=False, ensure_ascii=False)
        # return result

    def send_get(self, url, header, data):
        r = self.s.test_login().get(url=url, headers=header, params=data, verify=False)
        result = r.json()
        # print(type(result))
        return json.dumps(result, indent=2, sort_keys=False, ensure_ascii=False)
        # return result

    def run_main(self, url, method, header=None, data=None):
        r = None
        if method == 'GET':
            r = self.send_get(url, header, data)
        else:
            r = self.send_post(url, header, data)
        return r

3.测试一个接口

测试代码如下:

# coding:utf-8

import unittest
from common_util.run_method import RunMain  # 从之前封装的文件中,引入RunMain类


class ModuleList(unittest.TestCase):
    def setUp(self):
        self.run = RunMain()
        self.url = 'http://localhost:8088/consult/saveConsult.do'
        self.header = {
            "Host": "localhost.:8088",
            "Connection": "keep-alive",
            "Content-Length": "89",
            "Accept": "application/json, text/javascript, */*; q=0.01",
            "Origin": "http://localhost.:8088",
            "X-Requested-With": "XMLHttpRequest",
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36",
            "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
            "Referer": "http://localhost.:8088/ApprExclusive/appr/enterprise/info/consult/toConsultEdit.do",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.8"
        }

    def test01(self):
        """录入一条咨询"""
        data = {
            'clientCode': 'test',
            'topic': 'topic',
            'content': 'test',
            'resrcType': '0'
        }
        r = self.run.run_main(self.url, 'GET', self.header, data)

        print(r)


if __name__ == '__main__':
    unittest.main()

ok,到此结束。

转载于:https://www.cnblogs.com/hanmk/p/9101275.html

你可能感兴趣的:(绕过cookies进行登录并封装请求方法)