11.17.3 Example
Keywords: lib

##
11.17.3 Example

The following example demonstrates how to open a can of spam using the `spam` module.

      >>> import Cookie
      >>> C = Cookie.SimpleCookie()
      >>> C = Cookie.SerialCookie()
      >>> C = Cookie.SmartCookie()
      >>> C = Cookie.Cookie() # backwards compatible alias for SmartCookie
      >>> C = Cookie.SmartCookie()
      >>> C["fig"] = "newton"
      >>> C["sugar"] = "wafer"
      >>> C # generate HTTP headers
      Set-Cookie: sugar=wafer;
      Set-Cookie: fig=newton;
      >>> C = Cookie.SmartCookie()
      >>> C["rocky"] = "road"
      >>> C["rocky"]["path"] = "/cookie"
      >>> print C.output(header="Cookie:")
      Cookie: rocky=road; Path=/cookie;
      >>> print C.output(attrs=[], header="Cookie:")
      Cookie: rocky=road;
      >>> C = Cookie.SmartCookie()
      >>> C.load("chips=ahoy; vienna=finger") # load from a string (HTTP header)
      >>> C
      Set-Cookie: vienna=finger;
      Set-Cookie: chips=ahoy;
      >>> C = Cookie.SmartCookie()
      >>> C.load('keebler="E=everybody; L=\"Loves\"; fudge=\012;";')
      >>> C
      Set-Cookie: keebler="E=everybody; L=\"Loves\"; fudge=\012;";
      >>> C = Cookie.SmartCookie()
      >>> C["oreo"] = "doublestuff"
      >>> C["oreo"]["path"] = "/"
      >>> C
      Set-Cookie: oreo="doublestuff"; Path=/;
      >>> C = Cookie.SmartCookie()
      >>> C["twix"] = "none for you"
      >>> C["twix"].value
      'none for you'
      >>> C = Cookie.SimpleCookie()
      >>> C["number"] = 7 # equivalent to C["number"] = str(7)
      >>> C["string"] = "seven"
      >>> C["number"].value
      '7'
      >>> C["string"].value
      'seven'
      >>> C
      Set-Cookie: number=7;
      Set-Cookie: string=seven;
      >>> C = Cookie.SerialCookie()
      >>> C["number"] = 7
      >>> C["string"] = "seven"
      >>> C["number"].value
      7
      >>> C["string"].value
      'seven'
      >>> C
      Set-Cookie: number="I7\012.";
      Set-Cookie: string="S'seven'\012p1\012.";
      >>> C = Cookie.SmartCookie()
      >>> C["number"] = 7
      >>> C["string"] = "seven"
      >>> C["number"].value
      7
      >>> C["string"].value
      'seven'
      >>> C
      Set-Cookie: number="I7\012.";
      Set-Cookie: string=seven;