Discuss / Python / 练习

练习

Topic source

xian_wen

#1 Created at ... [Delete] [Delete and Lock User]
import asyncio

from aiohttp import web


async def index(request):
    await asyncio.sleep(0.5)
    return web.Response(body=b'<h1>Index</h1>', content_type='text/html')


async def hello(request):
    await asyncio.sleep(0.5)
    text = '<h1>Hello, %s!</h1>' % request.match_info['name']
    return web.Response(body=text.encode('utf-8'), content_type='text/html')


app = web.Application()
app.router.add_routes([web.get('/', index),
                       web.get('/hello/{name}', hello)])
web.run_app(app, host='localhost', port=8000)

xian_wen

#2 Created at ... [Delete] [Delete and Lock User]

以上为 Django 风格,下面为 Flask 风格:

import asyncio

from aiohttp import web

routes = web.RouteTableDef()


@routes.get('/')
async def index(request):
    await asyncio.sleep(0.5)
    return web.Response(body=b'<h1>Index</h1>', content_type='text/html')


@routes.get('/hello/{name}')
async def hello(request):
    await asyncio.sleep(0.5)
    text = '<h1>Hello, %s!</h1>' % request.match_info['name']
    return web.Response(body=text.encode('utf-8'), content_type='text/html')


app = web.Application()
app.add_routes(routes)
web.run_app(app, host='localhost', port=8000)

  • 1

Reply