如何在使用 aiohttp session.get 发出请求时发送 etag 或最后修改时间

How to send an etag or last modified while making a request with aiohttp session.get

如何发送 etag 和最后修改时间并模拟类似于上述库的行为?

更新 1

这里是一些详细的代码

import asyncio
import aiohttp

async def load_feed(session, url):
    # Keep this an empty string for the first request
    etag = 'fd31d1100c6390bd8a1f16d2703d56c0'
    # Keep this an empty string for the first request
    last_modified='Mon, 11 May 2020 22:27:44 GMT'
    try:
        async with session.get(url, headers={'etag': etag, 'Last-Modified': last_modified}) as response:
            t = await response.text()
            print(response.headers.get('etag'), response.headers.get('Last-Modified'), response.status, len(t), response.headers)
    except Exception as e:
        print(e)

async def load_feeds():
    try:
        async with aiohttp.ClientSession() as session:
            tasks = []
            for url in ['https://news.bitcoin.com/feed/']:
                task = asyncio.ensure_future(load_feed(session, url))
                tasks.append(task)
            await asyncio.gather(*tasks, return_exceptions=True)
    except:
        pass

asyncio.get_event_loop().run_until_complete(load_feeds())

期望:

发生了什么 - 我每次都收到状态代码 200 和完整响应 body

Last-Modified 是响应 header,对于您将使用 If-Modified-Since:

的请求
async def load_feed(session, url):
    headers = {
        "ETag": "fd31d1100c6390bd8a1f16d2703d56c0",
        "If-Modified-Since": "Mon, 11 May 2020 22:27:44 GMT"
    }
    try:
        async with session.get(url, headers=headers) as response:
            t = await response.text()
            print(response.headers.get("ETag"), response.headers.get('Last-Modified'), response.status, len(t))
    except Exception as e:
        print(e)

输出(通知status=304):

"fd31d1100c6390bd8a1f16d2703d56c0" Mon, 11 May 2020 22:27:44 GMT 304 0