Python cookiejar

来自百合仙子's Wiki
跳转到导航 跳转到搜索

Python 标准库里的 http.cookiejar 很难用。因此,这里列出一些常用的代码片断供参考。

代码片断

MozillaCookieJar 中删除特定的 cookie:

for c in tuple(jar):
  if c.name.startswith('___'):
    jar.clear(c.domain, c.path, c.name)
def make_cookie(name, value, expires=None, domain='', path='/'):
  '''
  returns a Cookie instance that you can add to a cookiejar

  expires: the time in seconds since epoch of time
  '''
  return http.cookiejar.Cookie(
    version=0, name=name, value=value, port=None, port_specified=False,
    domain=domain, domain_specified=False, domain_initial_dot=False,
    path=path, path_specified=True, secure=False, expires=expires,
    discard=None, comment=None, comment_url=None, rest={'HttpOnly': None},
    rfc2109=False,
  )

参见