廖雪峰Python3-实战Day2-编写Web App骨架的理解

2017-07-06  本文已影响95人  相关函数

笔者新手.记录学习,如有错误,欢迎指正,不胜感激.

源码如下:

import logging; logging.basicConfig(level=logging.INFO)

import asyncio, os, json, time
from datetime import datetime

from aiohttp import web

async def index(request):
    return web.Response(body=b'<h1> Awesome </h1>', content_type='text/html')

async def init(loop):
    app = web.Application(loop=loop)
    app.router.add_route('GET', '/', index)
    srv = await loop.create_server(app.make_handler(), '127.0.0.1', 9000)
    logging.info('server started at http://127.0.0.1:9000...')
    return srv

loop = asyncio.get_event_loop()
loop.run_until_complete(init(loop))
loop.run_forever()

导入相应模块

编写index

编写init

End

上一篇下一篇

猜你喜欢

热点阅读