我们的webapp基于aiohttp建立,写一个简单的app.py把网络环境先运行起来:
# -*- coding: UTF-8 -*- # Filename : app.py # author by : woodmanzhang.com import logging;logging.basicConfig(level=logging.INFO) import asyncio,os,json,time from datetime import datetime from aiohttp import web,web_runner routes = web.RouteTableDef() @routes.get('/') def index(request): return web.Response(body='<h1>Go!WoodmanZhang!</h1>', content_type='text/html') def init(): app = web.Application() app.add_routes([web.get('/', index)]) logging.info('server started at http://127.0.0.1:9000...') web.run_app(app, host='127.0.0.1', port=9000) init()
运行app.py,Web App将在9000端口监听HTTP请求,并且对首页进行响应:
/usr/local/bin/python3.8 ".../first web app/conf/app.py" INFO:root:server started at http://127.0.0.1:9000... ======== Running on http://127.0.0.1:9000 ======== (Press CTRL+C to quit)
我们直接去浏览器访问http://127.0.0.1:9000,就可以看到刚才编写的“Go!WoodmanZhang!”这段内容:
ok,骨架搭建完毕!
附:aiohttp的最新文档
https://docs.aiohttp.org/en/latest/