Http服务器
使用Sanic
库
from sanic import HTTPResponse, Sanic, json
初始化定义
app = Sanic('test')
设置路径
@app.route('/json')
async def json_return(request) :
begin = request.args.get('begin') #参数在args.get('name')里
if begin is None: #如果没有begin的参数,则为None
pass
运行
def main():
app.run(host = '127.0.0.1', port = 2054)
其他详细教程请跳转Sanic文档
Http请求
使用aiohttp
库进行请求
import asyncio
import aiohttp
访问方法
async def get_text(session, url) :
async with session.get(url) as response :
return await response.text( )
url = 'http://127.0.0.1:2054/path?param1=a&¶m2=b'
async with aiohttp.ClientSession() as session :
result = await get_text(session, url)
0 评论