Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/python27/lib64/python2....
File: _MozillaCookieJar.py
"""Mozilla / Netscape cookie loading / saving."""
[0] Fix | Delete
[1] Fix | Delete
import re, time
[2] Fix | Delete
[3] Fix | Delete
from cookielib import (_warn_unhandled_exception, FileCookieJar, LoadError,
[4] Fix | Delete
Cookie, MISSING_FILENAME_TEXT)
[5] Fix | Delete
[6] Fix | Delete
class MozillaCookieJar(FileCookieJar):
[7] Fix | Delete
"""
[8] Fix | Delete
[9] Fix | Delete
WARNING: you may want to backup your browser's cookies file if you use
[10] Fix | Delete
this class to save cookies. I *think* it works, but there have been
[11] Fix | Delete
bugs in the past!
[12] Fix | Delete
[13] Fix | Delete
This class differs from CookieJar only in the format it uses to save and
[14] Fix | Delete
load cookies to and from a file. This class uses the Mozilla/Netscape
[15] Fix | Delete
`cookies.txt' format. lynx uses this file format, too.
[16] Fix | Delete
[17] Fix | Delete
Don't expect cookies saved while the browser is running to be noticed by
[18] Fix | Delete
the browser (in fact, Mozilla on unix will overwrite your saved cookies if
[19] Fix | Delete
you change them on disk while it's running; on Windows, you probably can't
[20] Fix | Delete
save at all while the browser is running).
[21] Fix | Delete
[22] Fix | Delete
Note that the Mozilla/Netscape format will downgrade RFC2965 cookies to
[23] Fix | Delete
Netscape cookies on saving.
[24] Fix | Delete
[25] Fix | Delete
In particular, the cookie version and port number information is lost,
[26] Fix | Delete
together with information about whether or not Path, Port and Discard were
[27] Fix | Delete
specified by the Set-Cookie2 (or Set-Cookie) header, and whether or not the
[28] Fix | Delete
domain as set in the HTTP header started with a dot (yes, I'm aware some
[29] Fix | Delete
domains in Netscape files start with a dot and some don't -- trust me, you
[30] Fix | Delete
really don't want to know any more about this).
[31] Fix | Delete
[32] Fix | Delete
Note that though Mozilla and Netscape use the same format, they use
[33] Fix | Delete
slightly different headers. The class saves cookies using the Netscape
[34] Fix | Delete
header by default (Mozilla can cope with that).
[35] Fix | Delete
[36] Fix | Delete
"""
[37] Fix | Delete
magic_re = "#( Netscape)? HTTP Cookie File"
[38] Fix | Delete
header = """\
[39] Fix | Delete
# Netscape HTTP Cookie File
[40] Fix | Delete
# http://curl.haxx.se/rfc/cookie_spec.html
[41] Fix | Delete
# This is a generated file! Do not edit.
[42] Fix | Delete
[43] Fix | Delete
"""
[44] Fix | Delete
[45] Fix | Delete
def _really_load(self, f, filename, ignore_discard, ignore_expires):
[46] Fix | Delete
now = time.time()
[47] Fix | Delete
[48] Fix | Delete
magic = f.readline()
[49] Fix | Delete
if not re.search(self.magic_re, magic):
[50] Fix | Delete
f.close()
[51] Fix | Delete
raise LoadError(
[52] Fix | Delete
"%r does not look like a Netscape format cookies file" %
[53] Fix | Delete
filename)
[54] Fix | Delete
[55] Fix | Delete
try:
[56] Fix | Delete
while 1:
[57] Fix | Delete
line = f.readline()
[58] Fix | Delete
if line == "": break
[59] Fix | Delete
[60] Fix | Delete
# last field may be absent, so keep any trailing tab
[61] Fix | Delete
if line.endswith("\n"): line = line[:-1]
[62] Fix | Delete
[63] Fix | Delete
# skip comments and blank lines XXX what is $ for?
[64] Fix | Delete
if (line.strip().startswith(("#", "$")) or
[65] Fix | Delete
line.strip() == ""):
[66] Fix | Delete
continue
[67] Fix | Delete
[68] Fix | Delete
domain, domain_specified, path, secure, expires, name, value = \
[69] Fix | Delete
line.split("\t")
[70] Fix | Delete
secure = (secure == "TRUE")
[71] Fix | Delete
domain_specified = (domain_specified == "TRUE")
[72] Fix | Delete
if name == "":
[73] Fix | Delete
# cookies.txt regards 'Set-Cookie: foo' as a cookie
[74] Fix | Delete
# with no name, whereas cookielib regards it as a
[75] Fix | Delete
# cookie with no value.
[76] Fix | Delete
name = value
[77] Fix | Delete
value = None
[78] Fix | Delete
[79] Fix | Delete
initial_dot = domain.startswith(".")
[80] Fix | Delete
assert domain_specified == initial_dot
[81] Fix | Delete
[82] Fix | Delete
discard = False
[83] Fix | Delete
if expires == "":
[84] Fix | Delete
expires = None
[85] Fix | Delete
discard = True
[86] Fix | Delete
[87] Fix | Delete
# assume path_specified is false
[88] Fix | Delete
c = Cookie(0, name, value,
[89] Fix | Delete
None, False,
[90] Fix | Delete
domain, domain_specified, initial_dot,
[91] Fix | Delete
path, False,
[92] Fix | Delete
secure,
[93] Fix | Delete
expires,
[94] Fix | Delete
discard,
[95] Fix | Delete
None,
[96] Fix | Delete
None,
[97] Fix | Delete
{})
[98] Fix | Delete
if not ignore_discard and c.discard:
[99] Fix | Delete
continue
[100] Fix | Delete
if not ignore_expires and c.is_expired(now):
[101] Fix | Delete
continue
[102] Fix | Delete
self.set_cookie(c)
[103] Fix | Delete
[104] Fix | Delete
except IOError:
[105] Fix | Delete
raise
[106] Fix | Delete
except Exception:
[107] Fix | Delete
_warn_unhandled_exception()
[108] Fix | Delete
raise LoadError("invalid Netscape format cookies file %r: %r" %
[109] Fix | Delete
(filename, line))
[110] Fix | Delete
[111] Fix | Delete
def save(self, filename=None, ignore_discard=False, ignore_expires=False):
[112] Fix | Delete
if filename is None:
[113] Fix | Delete
if self.filename is not None: filename = self.filename
[114] Fix | Delete
else: raise ValueError(MISSING_FILENAME_TEXT)
[115] Fix | Delete
[116] Fix | Delete
f = open(filename, "w")
[117] Fix | Delete
try:
[118] Fix | Delete
f.write(self.header)
[119] Fix | Delete
now = time.time()
[120] Fix | Delete
for cookie in self:
[121] Fix | Delete
if not ignore_discard and cookie.discard:
[122] Fix | Delete
continue
[123] Fix | Delete
if not ignore_expires and cookie.is_expired(now):
[124] Fix | Delete
continue
[125] Fix | Delete
if cookie.secure: secure = "TRUE"
[126] Fix | Delete
else: secure = "FALSE"
[127] Fix | Delete
if cookie.domain.startswith("."): initial_dot = "TRUE"
[128] Fix | Delete
else: initial_dot = "FALSE"
[129] Fix | Delete
if cookie.expires is not None:
[130] Fix | Delete
expires = str(cookie.expires)
[131] Fix | Delete
else:
[132] Fix | Delete
expires = ""
[133] Fix | Delete
if cookie.value is None:
[134] Fix | Delete
# cookies.txt regards 'Set-Cookie: foo' as a cookie
[135] Fix | Delete
# with no name, whereas cookielib regards it as a
[136] Fix | Delete
# cookie with no value.
[137] Fix | Delete
name = ""
[138] Fix | Delete
value = cookie.name
[139] Fix | Delete
else:
[140] Fix | Delete
name = cookie.name
[141] Fix | Delete
value = cookie.value
[142] Fix | Delete
f.write(
[143] Fix | Delete
"\t".join([cookie.domain, initial_dot, cookie.path,
[144] Fix | Delete
secure, expires, name, value])+
[145] Fix | Delete
"\n")
[146] Fix | Delete
finally:
[147] Fix | Delete
f.close()
[148] Fix | Delete
[149] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function