- import aiohttp
- import json
- from sanic.response import stream
- async def post(url, data, headers=None):
- async with aiohttp.ClientSession(headers=headers) as session:
- result = await session.post(url, data=data)
- return await result.json()
- async def post_text_plain(url, data, headers=None):
- async with aiohttp.ClientSession(headers=headers) as session:
- result = await session.post(url, data=data)
- return await result.text()
- async def get(url, headers=None, **kwargs):
- async with aiohttp.ClientSession(headers=headers) as session:
- result = await session.get(url, data=kwargs)
- return await result.json()
- async def put(url, data, headers=None):
- async with aiohttp.ClientSession(headers=headers) as session:
- result = await session.put(url, data=data)
- return await result.json()
#python脚本代码中调用:
import asyncio
loop = asyncio.get_event_loop() result = loop.run_until_complete(post(url,json.dumps(data),headers={"Content-Type": "application/json"}))
loop.close()
本文介绍了一种使用Python的aiohttp库进行异步HTTP请求的方法。包括POST、GET、PUT等请求方式,并展示了如何在Python脚本中通过asyncio进行调用。
8395

被折叠的 条评论
为什么被折叠?



