python - add cookie to cookiejar

后端 未结 2 1853
梦毁少年i
梦毁少年i 2021-02-08 00:49

How do I create a cookie and add it to a CookieJar instance in python? I have all the info for the cookie (name, value, domain, path, etc) and I don\'t want to extract a new co

2条回答
  •  自闭症患者
    2021-02-08 01:45

    Looking at cookielib, you get:

    try:
        from cookielib import Cookie, CookieJar         # Python 2
    except ImportError:
        from http.cookiejar import Cookie, CookieJar    # Python 3
    cj = CookieJar()
    # Cookie(version, name, value, port, port_specified, domain, 
    # domain_specified, domain_initial_dot, path, path_specified, 
    # secure, discard, comment, comment_url, rest)
    c = Cookie(None, 'asdf', None, '80', '80', 'www.foo.bar', 
           None, None, '/', None, False, False, 'TestCookie', None, None, None)
    cj.set_cookie(c)
    print cj
    

    Gives:

    ]>
    

    There are no real sanity checks for the instantiation parameters. The ports have to be strings, not int.

提交回复
热议问题