The LWPCookieJar saves a sequence of "Set-Cookie3" lines. "Set-Cookie3" is the format used by the libwww-perl library, not known to be compatible with any browser, but which is easy to read and doesn't lose information about RFC 2965 cookies. Additional methods as_lwp_str(
| 1858 | return join_header_words([h]) |
| 1859 | |
| 1860 | class LWPCookieJar(FileCookieJar): |
| 1861 | """ |
| 1862 | The LWPCookieJar saves a sequence of "Set-Cookie3" lines. |
| 1863 | "Set-Cookie3" is the format used by the libwww-perl library, not known |
| 1864 | to be compatible with any browser, but which is easy to read and |
| 1865 | doesn't lose information about RFC 2965 cookies. |
| 1866 | |
| 1867 | Additional methods |
| 1868 | |
| 1869 | as_lwp_str(ignore_discard=True, ignore_expired=True) |
| 1870 | |
| 1871 | """ |
| 1872 | |
| 1873 | def as_lwp_str(self, ignore_discard=True, ignore_expires=True): |
| 1874 | """Return cookies as a string of "\\n"-separated "Set-Cookie3" headers. |
| 1875 | |
| 1876 | ignore_discard and ignore_expires: see docstring for FileCookieJar.save |
| 1877 | |
| 1878 | """ |
| 1879 | now = time.time() |
| 1880 | r = [] |
| 1881 | for cookie in self: |
| 1882 | if not ignore_discard and cookie.discard: |
| 1883 | continue |
| 1884 | if not ignore_expires and cookie.is_expired(now): |
| 1885 | continue |
| 1886 | r.append("Set-Cookie3: %s" % lwp_cookie_str(cookie)) |
| 1887 | return "\n".join(r+[""]) |
| 1888 | |
| 1889 | def save(self, filename=None, ignore_discard=False, ignore_expires=False): |
| 1890 | if filename is None: |
| 1891 | if self.filename is not None: filename = self.filename |
| 1892 | else: raise ValueError(MISSING_FILENAME_TEXT) |
| 1893 | |
| 1894 | with os.fdopen( |
| 1895 | os.open(filename, os.O_CREAT | os.O_WRONLY | os.O_TRUNC, 0o600), |
| 1896 | 'w', |
| 1897 | ) as f: |
| 1898 | # There really isn't an LWP Cookies 2.0 format, but this indicates |
| 1899 | # that there is extra information in here (domain_dot and |
| 1900 | # port_spec) while still being compatible with libwww-perl, I hope. |
| 1901 | f.write("#LWP-Cookies-2.0\n") |
| 1902 | f.write(self.as_lwp_str(ignore_discard, ignore_expires)) |
| 1903 | |
| 1904 | def _really_load(self, f, filename, ignore_discard, ignore_expires): |
| 1905 | magic = f.readline() |
| 1906 | if not self.magic_re.search(magic): |
| 1907 | msg = ("%r does not look like a Set-Cookie3 (LWP) format " |
| 1908 | "file" % filename) |
| 1909 | raise LoadError(msg) |
| 1910 | |
| 1911 | now = time.time() |
| 1912 | |
| 1913 | header = "Set-Cookie3:" |
| 1914 | boolean_attrs = ("port_spec", "path_spec", "domain_dot", |
| 1915 | "secure", "discard") |
| 1916 | value_attrs = ("version", |
| 1917 | "port", "path", "domain", |
no outgoing calls
searching dependent graphs…