我用极少的代码了实现一款 web 框架,目标是用低于 1000 行的代码实现 flask 的核心功能, xweb 框架基于 python3.5 以上开发,实在是居家旅行良品.
我的目标是用最少的代码实现符合现状的 web 框架,欢迎同样有兴趣的同学一起参与进来
说明
基于3.5开发
安装
pip install xweb
基本用法
from xweb.application import XWeb
app = XWeb()
@app.route('/:name/')
def call_my_name(name):
return 'hi {}!'.format(name)
app.listen(3000)
请求与相应
from xweb.globals import request
request.path
request.query_string
request.query
request.files
request.forms
request.json
request.ip
request.hostname
request.headers
from xweb.globals import response
response.headers
response.status
response.body
中间件
from xweb.application import XWeb
app = XWeb()
@app.middleware('request')
def print_on_request1():
print("I print when a request is received by the server1")
@app.middleware('request')
def print_on_request2():
print("I print when a request is received by the server2")
@app.middleware('response')
def print_on_response1():
print("I print when a response is returned by the server1")
@app.middleware('response')
def print_on_response2():
print("I print when a response is returned by the server2")
@app.route('/:name/')
def call_my_name(name):
return 'hi {}!'.format(name)
app.listen(3000)
我的目标是用最少的代码实现符合现状的 web 框架,欢迎同样有兴趣的同学一起参与进来