通过pickle aiohttp保存cookie

问题描述 投票:0回答:1

我正在为当前版本的 Steam 重写库

asyncsteampy
。但是当我尝试将会话保存到 pickle 文件时,出现错误

async def create_steam_session(username, password, steam_guard, pkl_name, api_key, proxies):
    steam_client = SteamClient(username, password, f'steam_guards/{steam_guard}', api_key=api_key)
    await steam_client.login()
    return steam_client

async def authorization(username, password, steam_guard, pkl_name, api_key):
    steam_client = await create_steam_session(username, password, steam_guard, pkl_name, api_key)
    await steam_client._session.connector.close()
    with open(f'async_pkl/{pkl_name}', 'wb') as f:
        pickle.dump(steam_client, f)

如果我不使用

await steam_client._session.connector.close()
我会收到错误
TypeError: cannot pickle 'weakref' object
我以前只是删除它
'weakref' object
但是然后出现以下错误
TypeError: cannot pickle '_contextvars.Context' object
但是在
await steam_client._session.connector.close()
之后出现以下错误
TypeError: cannot pickle '_queue.SimpleQueue' object

如何解决错误

'_queue.SimpleQueue' object

python queue python-asyncio pickle aiohttp
1个回答
0
投票

你试图腌制整个客户,而不是饼干。这不起作用,因为客户端有开放的套接字和其他永远无法腌制的东西。

我不知道那个特定的库公开了什么,但是

aiohttp.ClientSession
有一个
.save()
.load()
的cookie方法: https://docs.aiohttp.org/en/stable/client_reference.html#aiohttp.CookieJar.save

请注意,这些在 aiohttp 的次要版本中并不稳定,因此如果没有转换脚本,3.9 中保存的 cookie 可能无法在 3.10 中加载。

© www.soinside.com 2019 - 2024. All rights reserved.