如何通过aiohttp session.get发送请求时发送etag或最后修改的内容

问题描述 投票:1回答:1
  • 我每分钟都加载多个供稿网址
  • 我想发送一个http get请求,如果自上次加载以来数据已更改,则获得200个状态代码和完整数据
  • 如果自上次加载以来数据未更改,我需要http状态码304,并且没有响应正文
  • 如果我使用Python feedparser的库来发送GET请求,它会提供HERE此功能
  • 如何使用aiohttp库执行此操作
  • 我如何发送etag并在GET请求中进行最后修改

    async with session.get(url) as response:
        text = await response.text()
        print(response.headers.get('etag'), response.headers.get('Last-Modified'))
    

我如何发送etag并进行最后修改并模拟与上述库相似的行为?

UPDATE 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())

期望:

  • 第一次发送没有标题的请求
  • 获得带有etag的响应代码200和上次修改的完整响应
  • 使用etag再次发送请求并最后修改
  • 获得没有响应正文的响应代码304

正在发生什么-我每次都会收到状态码200和完整的响应正文

python-3.x python-asyncio aiohttp etag last-modified
1个回答
1
投票

[Last-Modified是响应头,对于请求,您将使用If-Modified-Since

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