因爲咱們的Web App創建在asyncio的基礎上,所以用aiohttp寫一個基本的app.py
:javascript
import logging; logging.basicConfig(level=logging.INFO) import asyncio, os, json, time from datetime import datetime from aiohttp import web def index(request): return web.Response(body=b'<h1>Awesome</h1>') @asyncio.coroutine def init(loop): app = web.Application(loop=loop) app.router.add_route('GET', '/', index) srv = yield from 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()
運行python app.py
,Web App將在9000
端口監聽HTTP請求,而且對首頁/
進行響應:java
$ python3 app.py
INFO:root:server started at http://127.0.0.1:9000...
這裏咱們簡單地返回一個Awesome
字符串,在瀏覽器中能夠看到效果:python
這說明咱們的Web App骨架已經搭好了,能夠進一步往裏面添加更多的東西。web