可以将cookiejar对象腌制吗?

问题描述:

我试图像这样腌制CookieJar对象:

I tried pickling a CookieJar object like this:

import cookielib
import pickle

dumpFile = open('cookie.dump','w')
cj = cookielib.CookieJar()
pickle.dump(cj, dumpFile)

它引发了以下异常:

raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle lock objects

可以给CookieJar腌吗?

Can a CookieJar be pickled?

所问问题的答案为否":罐子本身不能腌制.

The answer to the question as asked is "no": the jar itself is not pickle-able.

但是,包含在罐子中的饼干:

pickle.dump([c for c in cj], dumpFile)

例如,

可以解决问题. (然后,您可以加载结果并将cookie列表插入新的jar中.您可能要先检查它们是否已过期,但是首先要检查它们的到期时间.根据您进行酸洗的时间,甚至可能希望在转储之前进行检查. )

will do the trick, for instance. (You can then load the result and insert the list of cookies into a new jar. You will probably want to check them for expiration and such first though. Depending on when you're doing the pickling you might even want to check before dumping.)