我如何发送etag并在GET请求中最后修改
async with session.get(url) as response:
text = await response.text()
print(response.headers.get('etag'), response.headers.get('Last-Modified'))
如何发送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和完整的响应正文
Last-Modified
是响应标头,对于请求,您可以使用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)
输出(通知状态= 304):
"fd31d1100c6390bd8a1f16d2703d56c0" Mon, 11 May 2020 22:27:44 GMT 304 0
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句