如何。将参数和标头传递给`aiohttp.ClientSession` python

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

我希望将paramsheaders传递给aiohttp.ClientSession,如here所示。

这是我尝试过的:

    async def make_request(self, url, headers, params):
        async with aiohttp.ClientSession(headers=headers, params=params) as session:
            async with self.limit, session.get(url=url) as response:
                await asyncio.sleep(self.rate)
                resp = await response.read()
                return resp
async def process(url, url_id, update_id, rate, limit):
    limit = asyncio.BoundedSemaphore(limit)

    f = Fetch(
        rate=rate,
        limit=limit,
    )

    if "coinmarketcap" in url:
        params = {
            'start': '1',
            'limit': '1',
            'convert': 'USD,BTC'
        }
        headers = {
            'Accepts': 'application/json',
            'X-CMC_PRO_API_KEY': API_KEY,
        }
    else:
        params = {}
        headers = {}

    result = await f.make_request(url, headers, params)

但是我得到了错误:

Unexpected Argument at:
async with aiohttp.ClientSession(headers=headers, params=params) as session

如果URL设为coinmarketcap,否则设为no params/headers,我希望设置标题。如何解决?

python-3.x python-requests python-asyncio aiohttp
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.