Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/python27/lib64/python2....
File: _LWPCookieJar.py
"""Load / save to libwww-perl (LWP) format files.
[0] Fix | Delete
[1] Fix | Delete
Actually, the format is slightly extended from that used by LWP's
[2] Fix | Delete
(libwww-perl's) HTTP::Cookies, to avoid losing some RFC 2965 information
[3] Fix | Delete
not recorded by LWP.
[4] Fix | Delete
[5] Fix | Delete
It uses the version string "2.0", though really there isn't an LWP Cookies
[6] Fix | Delete
2.0 format. This indicates that there is extra information in here
[7] Fix | Delete
(domain_dot and # port_spec) while still being compatible with
[8] Fix | Delete
libwww-perl, I hope.
[9] Fix | Delete
[10] Fix | Delete
"""
[11] Fix | Delete
[12] Fix | Delete
import time, re
[13] Fix | Delete
from cookielib import (_warn_unhandled_exception, FileCookieJar, LoadError,
[14] Fix | Delete
Cookie, MISSING_FILENAME_TEXT,
[15] Fix | Delete
join_header_words, split_header_words,
[16] Fix | Delete
iso2time, time2isoz)
[17] Fix | Delete
[18] Fix | Delete
def lwp_cookie_str(cookie):
[19] Fix | Delete
"""Return string representation of Cookie in the LWP cookie file format.
[20] Fix | Delete
[21] Fix | Delete
Actually, the format is extended a bit -- see module docstring.
[22] Fix | Delete
[23] Fix | Delete
"""
[24] Fix | Delete
h = [(cookie.name, cookie.value),
[25] Fix | Delete
("path", cookie.path),
[26] Fix | Delete
("domain", cookie.domain)]
[27] Fix | Delete
if cookie.port is not None: h.append(("port", cookie.port))
[28] Fix | Delete
if cookie.path_specified: h.append(("path_spec", None))
[29] Fix | Delete
if cookie.port_specified: h.append(("port_spec", None))
[30] Fix | Delete
if cookie.domain_initial_dot: h.append(("domain_dot", None))
[31] Fix | Delete
if cookie.secure: h.append(("secure", None))
[32] Fix | Delete
if cookie.expires: h.append(("expires",
[33] Fix | Delete
time2isoz(float(cookie.expires))))
[34] Fix | Delete
if cookie.discard: h.append(("discard", None))
[35] Fix | Delete
if cookie.comment: h.append(("comment", cookie.comment))
[36] Fix | Delete
if cookie.comment_url: h.append(("commenturl", cookie.comment_url))
[37] Fix | Delete
[38] Fix | Delete
keys = cookie._rest.keys()
[39] Fix | Delete
keys.sort()
[40] Fix | Delete
for k in keys:
[41] Fix | Delete
h.append((k, str(cookie._rest[k])))
[42] Fix | Delete
[43] Fix | Delete
h.append(("version", str(cookie.version)))
[44] Fix | Delete
[45] Fix | Delete
return join_header_words([h])
[46] Fix | Delete
[47] Fix | Delete
class LWPCookieJar(FileCookieJar):
[48] Fix | Delete
"""
[49] Fix | Delete
The LWPCookieJar saves a sequence of "Set-Cookie3" lines.
[50] Fix | Delete
"Set-Cookie3" is the format used by the libwww-perl library, not known
[51] Fix | Delete
to be compatible with any browser, but which is easy to read and
[52] Fix | Delete
doesn't lose information about RFC 2965 cookies.
[53] Fix | Delete
[54] Fix | Delete
Additional methods
[55] Fix | Delete
[56] Fix | Delete
as_lwp_str(ignore_discard=True, ignore_expired=True)
[57] Fix | Delete
[58] Fix | Delete
"""
[59] Fix | Delete
[60] Fix | Delete
def as_lwp_str(self, ignore_discard=True, ignore_expires=True):
[61] Fix | Delete
"""Return cookies as a string of "\\n"-separated "Set-Cookie3" headers.
[62] Fix | Delete
[63] Fix | Delete
ignore_discard and ignore_expires: see docstring for FileCookieJar.save
[64] Fix | Delete
[65] Fix | Delete
"""
[66] Fix | Delete
now = time.time()
[67] Fix | Delete
r = []
[68] Fix | Delete
for cookie in self:
[69] Fix | Delete
if not ignore_discard and cookie.discard:
[70] Fix | Delete
continue
[71] Fix | Delete
if not ignore_expires and cookie.is_expired(now):
[72] Fix | Delete
continue
[73] Fix | Delete
r.append("Set-Cookie3: %s" % lwp_cookie_str(cookie))
[74] Fix | Delete
return "\n".join(r+[""])
[75] Fix | Delete
[76] Fix | Delete
def save(self, filename=None, ignore_discard=False, ignore_expires=False):
[77] Fix | Delete
if filename is None:
[78] Fix | Delete
if self.filename is not None: filename = self.filename
[79] Fix | Delete
else: raise ValueError(MISSING_FILENAME_TEXT)
[80] Fix | Delete
[81] Fix | Delete
f = open(filename, "w")
[82] Fix | Delete
try:
[83] Fix | Delete
# There really isn't an LWP Cookies 2.0 format, but this indicates
[84] Fix | Delete
# that there is extra information in here (domain_dot and
[85] Fix | Delete
# port_spec) while still being compatible with libwww-perl, I hope.
[86] Fix | Delete
f.write("#LWP-Cookies-2.0\n")
[87] Fix | Delete
f.write(self.as_lwp_str(ignore_discard, ignore_expires))
[88] Fix | Delete
finally:
[89] Fix | Delete
f.close()
[90] Fix | Delete
[91] Fix | Delete
def _really_load(self, f, filename, ignore_discard, ignore_expires):
[92] Fix | Delete
magic = f.readline()
[93] Fix | Delete
if not re.search(self.magic_re, magic):
[94] Fix | Delete
msg = ("%r does not look like a Set-Cookie3 (LWP) format "
[95] Fix | Delete
"file" % filename)
[96] Fix | Delete
raise LoadError(msg)
[97] Fix | Delete
[98] Fix | Delete
now = time.time()
[99] Fix | Delete
[100] Fix | Delete
header = "Set-Cookie3:"
[101] Fix | Delete
boolean_attrs = ("port_spec", "path_spec", "domain_dot",
[102] Fix | Delete
"secure", "discard")
[103] Fix | Delete
value_attrs = ("version",
[104] Fix | Delete
"port", "path", "domain",
[105] Fix | Delete
"expires",
[106] Fix | Delete
"comment", "commenturl")
[107] Fix | Delete
[108] Fix | Delete
try:
[109] Fix | Delete
while 1:
[110] Fix | Delete
line = f.readline()
[111] Fix | Delete
if line == "": break
[112] Fix | Delete
if not line.startswith(header):
[113] Fix | Delete
continue
[114] Fix | Delete
line = line[len(header):].strip()
[115] Fix | Delete
[116] Fix | Delete
for data in split_header_words([line]):
[117] Fix | Delete
name, value = data[0]
[118] Fix | Delete
standard = {}
[119] Fix | Delete
rest = {}
[120] Fix | Delete
for k in boolean_attrs:
[121] Fix | Delete
standard[k] = False
[122] Fix | Delete
for k, v in data[1:]:
[123] Fix | Delete
if k is not None:
[124] Fix | Delete
lc = k.lower()
[125] Fix | Delete
else:
[126] Fix | Delete
lc = None
[127] Fix | Delete
# don't lose case distinction for unknown fields
[128] Fix | Delete
if (lc in value_attrs) or (lc in boolean_attrs):
[129] Fix | Delete
k = lc
[130] Fix | Delete
if k in boolean_attrs:
[131] Fix | Delete
if v is None: v = True
[132] Fix | Delete
standard[k] = v
[133] Fix | Delete
elif k in value_attrs:
[134] Fix | Delete
standard[k] = v
[135] Fix | Delete
else:
[136] Fix | Delete
rest[k] = v
[137] Fix | Delete
[138] Fix | Delete
h = standard.get
[139] Fix | Delete
expires = h("expires")
[140] Fix | Delete
discard = h("discard")
[141] Fix | Delete
if expires is not None:
[142] Fix | Delete
expires = iso2time(expires)
[143] Fix | Delete
if expires is None:
[144] Fix | Delete
discard = True
[145] Fix | Delete
domain = h("domain")
[146] Fix | Delete
domain_specified = domain.startswith(".")
[147] Fix | Delete
c = Cookie(h("version"), name, value,
[148] Fix | Delete
h("port"), h("port_spec"),
[149] Fix | Delete
domain, domain_specified, h("domain_dot"),
[150] Fix | Delete
h("path"), h("path_spec"),
[151] Fix | Delete
h("secure"),
[152] Fix | Delete
expires,
[153] Fix | Delete
discard,
[154] Fix | Delete
h("comment"),
[155] Fix | Delete
h("commenturl"),
[156] Fix | Delete
rest)
[157] Fix | Delete
if not ignore_discard and c.discard:
[158] Fix | Delete
continue
[159] Fix | Delete
if not ignore_expires and c.is_expired(now):
[160] Fix | Delete
continue
[161] Fix | Delete
self.set_cookie(c)
[162] Fix | Delete
[163] Fix | Delete
except IOError:
[164] Fix | Delete
raise
[165] Fix | Delete
except Exception:
[166] Fix | Delete
_warn_unhandled_exception()
[167] Fix | Delete
raise LoadError("invalid Set-Cookie3 format file %r: %r" %
[168] Fix | Delete
(filename, line))
[169] Fix | Delete
[170] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function