如何在 http.cookiejar 中制作自定义 cookie 属性?
How do I make custom cookie attributes in http.cookiejar?
我正在尝试向我的 python http 请求添加一个 cookie,如下所示:
{
"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
先谢谢你
制作一个 class 像下面这样的 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()
我正在尝试向我的 python http 请求添加一个 cookie,如下所示:
{
"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
先谢谢你
制作一个 class 像下面这样的 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()