我试图添加一个cookie到我的python http请求,看起来像下面的东西:
{
"domain": ".foo.com",
"hostOnly": false,
"httpOnly": false,
"name": "name",
"path": "/",
"sameSite": null,
"secure": false,
"session": true,
"storeId": null,
"value": "none"
}
当我做这样的事情时,
jar = cookielib.CookieJar()
cookies = json.loads(open('cookies.json', 'r').read())
for i in cookies:
print(i['expirationDate'])
jar.set_cookie(i['name'], expirationDate = i['expirationDate'], hostOnly=i["hostOnly"], httpOnly = i["httpOnly"], sameSite = i["sameSite"], secure = i["secure"], session = i["session"], storeId = i["storeId"], value = i["value"])
抛出以下错误:
Traceback (most recent call last):
File "/Applications/PyCharm.app/Contents/bin/Users/{me}/PycharmProjects/{project name}/{file name}.py", line {line}, in <module>
jar.set_cookie(i['name'], hostOnly=i["hostOnly"], httpOnly = i["httpOnly"], sameSite = i["sameSite"], secure = i["secure"], session = i["session"], storeId = i["storeId"], value = i["value"])
TypeError: set_cookie() got an unexpected keyword argument 'hostOnly'
我想知道如何添加一个具有特殊属性的cookie
in advanced
为如下的cookie创建一个类
import time
class cookie:
def __init__(self, name, version, path, domain, expirationDate, hostOnly, httpOnly, sameSite, secure, session, storeId, value):
self.name = name
self.version = version
self.path = path
self.domain = domain
self.expirationDate = expirationDate
self.hostOnly = hostOnly
self.httpOnly = httpOnly
self.sameSite = sameSite
self.secure = secure
self.session = session
self.storeId = storeId
self.value = value
def is_expired(self, now):
if now > time.time():
return True
return False
你可以将任何有效的cookie对象传入jar.set_cookie()