Putting a `Cookie` in a `CookieJar`

前端 未结 10 1465
谎友^
谎友^ 2020-11-30 01:45

I\'m using the new Python Requests library to make http requests. I obtain a cookie from the server as text. How do I turn that into a CookieJar with the cookie

10条回答
  •  孤城傲影
    2020-11-30 02:17

    I'm trying to do the same thing. This is what I have so far, and for some reason it isn't sending the cookies along in the header. It might get you far enough along to solve your problem though.

    import requests
    import cookielib
    import logging
    
    log = logging.getLogger(__name__)
    
    def auth(auth_url, cookies):
        cj = cookielib.CookieJar()
        for x in cookies:
             if len(cookies[x]) > 0:
                 ck = cookielib.Cookie(version=1, name=x, value=cookies[x], 
                        port=None, port_specified=False, domain='.example.com', 
                        domain_specified=True, 
                        domain_initial_dot=True, path='/', 
                        path_specified=True, secure=False, 
                        expires=None, discard=True, 
                        comment=None, comment_url=None, 
                        rest=None, rfc2109=True)
                 log.info(ck)
                 cj.set_cookie(ck)
    
        log.info("cookies = %s " % cj)
        response = requests.get(auth_url, cookies=cj)
        log.info("response %s \n" % response)
        log.info("response.headers %s \n" % response.headers)
        log.info("response.content %s \n" % response.content)
    

提交回复
热议问题