aiohttp 봉인 http 요청

1535 단어
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()

좋은 웹페이지 즐겨찾기