Python 封装requests使每次请求携带cookies

直接上代码:

class APIRequests(object):

    def __init__(self):
        pass

    def __getattr__(self, attr_name):
        if attr_name == 'cookies':
            cookies = cache.get(settings.SSO_SESSION_COOKIES)
            if not cookies:
                request_session = requests.session()
                request_session.post(settings.SSO_LOGIN_URL, json={
                    "username": settings.SSO_USERNAME,
                    "password": settings.SSO_PASSWORD
                })
                cookies = dict(request_session.cookies.items())
                cache.set(settings.SSO_SESSION_COOKIES, cookies, timeout=settings.SSO_TIME_OUT)
            return cookies
        return None

    def get(self, *args, **kwargs):
        return requests.get(cookies=self.cookies, *args, **kwargs)

    def post(self, *args, **kwargs):
        return requests.get(cookies=self.cookies, *args, **kwargs)

    def patch(self, *args, **kwargs):
        return requests.patch(cookies=self.cookies, *args, **kwargs)

    def put(self, *args, **kwargs):
        return requests.put(cookies=self.cookies, *args, **kwargs)

    def delete(self, *args, **kwargs):
        return requests.delete(cookies=self.cookies, *args, **kwargs)

api_requests = APIRequests()

调用:

from utils import api_requests

result = api_requests.get(url=url, params=params)

你可能感兴趣的:(Python 封装requests使每次请求携带cookies)