简体   繁体   English

如何在 MozillaCookieJar 加载 cookies.txt 之前删除 `#HttpOnly_` 前缀?

[英]How to remove `#HttpOnly_` prefix before MozillaCookieJar load cookies.txt?

I use Firefox's extension (cookies.txt) export cookies.txt for python script.我对 python 脚本使用 Firefox 的扩展 (cookies.txt) export cookies.txt。 And there is some HttpOnly cookie begins with "#HttpOnly_" was ignored in MozillaCookieJar, just like a comment, eg:还有一些以“#HttpOnly_”开头的 HttpOnly cookie 在 MozillaCookieJar 中被忽略,就像注释一样,例如:

#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:这是一个问题跟踪器:MozillaCookieJar 忽略 HttpOnly cookies并带有一些代码片段可能会解决如下问题:

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 ?如何修复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? open() function 不能接受生成器作为参数,是否有一些用于open() function 的装饰器或包装器可以接受生成器作为参数?

You can solve the issue by modifying the following line:您可以通过修改以下行来解决此问题:

MozillaCookieJar(filename=fix_cookie_jar_file('d:/cookies.txt'))

To this:对此:

MozillaCookieJar(filename=next(fix_cookie_jar_file('d:/cookies.txt')))

Here, we are using next to iterate over the elements of the generator, as the first element will be the name of the temporary file, it will work for the MozillaCookieJar as it will be receiving just a string instead of the generator itself.在这里,我们使用next来遍历生成器的元素,因为第一个元素将是临时文件的名称,它将适用于MozillaCookieJar ,因为它将只接收一个字符串而不是生成器本身。

Hope it helps!希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM