Python实战-编写Web App-Day2-编写Web App骨架

Python实战 木人张 3年前 (2020-04-11) 987次浏览 0个评论 扫描二维码

我们的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/


木人张,版权所有丨如未注明 , 均为原创,禁止转载。
喜欢 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址