Day 2 - 编写Web App骨架
2023-02-18 15:31:10 时间
由于我们的Web App建立在asyncio的基础上,因此用aiohttp写一个基本的app.py
:
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请求,并且对首页/
进行响应:
$ python3 app.py
INFO:root:server started at http://127.0.0.1:9000...
这里我们简单地返回一个Awesome
字符串,在浏览器中可以看到效果:
这说明我们的Web App骨架已经搭好了,可以进一步往里面添加更多的东西。
参考源码
相关文章
- 什么是MircoPython?
- [Python] 函数基本
- [Python] 使用dict和set
- [Python] Python基础字符串
- Python 绑定:从 Python 调用 C 或 C++
- Python 没有函数重载?如何用装饰器实现函数重载?
- [oeasy]python0028_直接运行_修改py文件执行权限_设置py文件打开方式
- 这15个顶级Python库,你必须要试试!
- ikun必学!python 画一个简单的只因
- 技术实践:教你用Python搭建gRPC服务
- 带你掌握4种Python 排序算法
- 一文讲全了Python 类和对象内容
- 什么是Python中的套接字编程?
- [Python]介绍
- [Python]第一章(建议收藏)
- 【Python】第二章(条件语句和循环语句)
- [Python]第三章(字符串) ---【建议收藏】
- (一)Python基本语法元素
- (二)Python基础结构
- (四)Python: NumPy中的ndarry