我如何发送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())
期望:
正在发生什么-我每次都会收到状态码200和完整的响应正文
[Last-Modified
是响应头,对于请求,您将使用If-Modified-Since
: