request库之会话保持

 

通过cookie 保持会话

import requests
'''
接口响应包含cookie 才能使用cookies 快速访问,
cookie 实际存在respons 的headers 里的Set-Cookie
Cookie 的返回对象为 RequestsCookieJar,它的行为和字典类似
'''

class test_demo:
        cookie = None

        def login(self):
                url = 'https://www.processon.com/login/quick_login'

                data = {'type': 'account_login',
                        'login_email': 15557548962, #账户密码自行更改
                        'login_password': 'qa1234',
                        }
                r = requests.request('post', url=url, data=data)
                test_demo.cookie=r.cookies

        def test_login(self):
                url = 'https://www.processon.com/setting/account'

                r = requests.request('post',url=url,cookies=test_demo.cookie)
                print(r.json())


if __name__ == '__main__':
        t = test_demo()
        #通过账户密码登录,获取cookie后传递给下一个接口
        t.login()
        #获取到cookie进行下一步操作
        t.test_login()

 

通过session保持会话

import requests,pytest


class Test_demo:
        session_a = requests.session()
        # session_a = requests.sessions.Session
        def test_login(self):
                url = 'https://www.processon.com/login/quick_login'
                data = {'type': 'account_login',
                        'login_email': 123456,
                        'login_password': '123456',
                        }
                r = Test_demo.session_a.request('post', url=url, data=data)
                print(r.json())
                print(r.cookies)
        
        def test_info(self):
                
        
                url = 'https://www.processon.com/setting/account'
        
                r = Test_demo.session_a.request('post',url=url)
                print(r.json())
                print(r.cookies)
                
                

if __name__ == '__main__':
        # t = Test_demo()
        # t.login()
        # t.test_login()
        pytest.main(['-vs','./request库之cookie鉴权请求.py'])

 

上一篇:Mybatis源码学习(二)简单Demo的运行原理


下一篇:实战OpenPose项目3:pytorch 实现openpose(包括手和身体姿态估计)