用到知识点:logging、asyncio、aiohttp模块(后续补充知识点)
""" app.py编写Web App骨架 """ import logging; logging.basicConfig(level=logging.INFO) import asyncio from aiohttp import web async def index(request): """响应函数""" return web.Response(body=b‘<h1>Awesome</h1>‘, content_type=‘text/html‘) async def init(loop): """Web App服务器初始化""" # 制作响应合集 app = web.Application(loop=loop) # 把响应函数添加到响应函数集合 app.router.add_route(method=‘GET‘, path=‘/‘, handler=index) # 创建服务器(连接网址、端口,绑定handler) srv = await loop.create_server(app.make_handler(), ‘127.0.0.1‘, 9000) logging.info(‘server start at http://127.0.0.1:9000‘) return srv # 创建事件 loop = asyncio.get_event_loop() # 运行 loop.run_until_complete(init(loop)) # 服务器不关闭 loop.run_forever()
时间: 2024-10-29 19:07:47