I use Firefox's extension (cookies.txt) export cookies.txt for python script. And there is some HttpOnly cookie begins with "#HttpOnly_" was ignored in MozillaCookieJar, just like a comment, e.g.:
#HttpOnly_.sample.com TRUE / FALSE 1258200001 value1 111
#HttpOnly_.sample.com FALSE / FALSE 1209905939 value2 222
Here is a issue tracker : MozillaCookieJar ignores HttpOnly cookies with some code snippets maybe fix the problem like this:
from tempfile import NamedTemporaryFile
from http.cookiejar import MozillaCookieJar
from contextlib import contextmanager
def fix_cookie_jar_file(orig_cookiejarfile):
with NamedTemporaryFile(mode='w+') as cjf:
with open(orig_cookiejarfile, 'r') as ocf:
for l in ocf:
cjf.write(l[10:] if l.startswith('#HttpOnly_') else l)
cjf.seek(0)
yield cjf.name
# the following code is TypeError: expected str, bytes or os.PathLike object, not generator
# Ref: https://bugs.python.org/issue2190
MozillaCookieJar(filename=fix_cookie_jar_file('d:/cookies.txt'))
How can I fix the TypeError
?
Thanks.
update: the follow code works as expected.
from tempfile import NamedTemporaryFile
from http.cookiejar import MozillaCookieJar
from contextlib import contextmanager
import os
# Ref: https://bugs.python.org/issue2190
def fix_cookie_jar_file(orig_cookiejarfile):
with NamedTemporaryFile(mode='w+', delete=False) as cjf:
with open(orig_cookiejarfile, 'r') as ocf:
for l in ocf:
cjf.write(l[10:] if l.startswith('#HttpOnly_') else l)
cjf.seek(0)
return cjf.name
filename = fix_cookie_jar_file(r"d:\cookies.txt")
jar = MozillaCookieJar(filename)
jar.load(ignore_expires = True)
# delete "manually" afterwards
os.remove(filename)
i=0
for ck in jar:
print("(%d) %s : %s"%(i,ck.name,ck.value))
i+=1
open()
function can't accept generator as parameter, is there some decorator or wrapper for open()
function can accept generator as parameter?