Python開源:Xweb-為 Python 之禪寫的極簡主義 Web 框架
Why use xweb
Your life will be long.
- Xweb has very simple api.
- Xweb did not use any third-party dependency.
- Xweb does not have any redundant code for python2.
- Source code is easy to understand.
- It is easy to write xweb's middleware.
Installation
pip install xweb
Hello world
from xweb.application import XWeb
app = XWeb()
@app.route('/')
def hello():
return 'hello world!'
app.listen(3000)</code></pre>
Route
from xweb.application import XWeb
app = XWeb()
@app.route('/:name/',methods=['GET','POST'])
def call_my_name(name):
return 'hi {}!'.format(name)
@app.post('/post/')
def post():
return 'hi post!'
@app.get('/get/')
def get():
return 'hi get!'
@app.put('/put/')
def put():
return 'hi put!'
@app.patch('/patch/')
def patch():
return 'hi patch!'
@app.delete('/delete/')
def delete():
return 'hi delete!'
app.listen(3000)</code></pre>
Request
from xweb.globals import request
request.path
request.query_string
request.query
request.files
request.forms
request.json
request.post
request.ip
request.hostname
request.headers
request.host
request.protocol
request.body
request.content_type
request.content_length</code></pre>
Response
from xweb.globals import response
response.headers
response.status
response.body</code></pre>
Middleware
from xweb.application import XWeb
app = XWeb()
@app.middleware('request')
def print_on_request():
print("I print when a request is received by the server")
@app.middleware('response')
def print_on_response():
print("I print when a response is returned by the server")
@app.route('/:name/')
def call_my_name(name):
return 'hi {}!'.format(name)
app.listen(3000)</code></pre>
Exception
from xweb.application import XWeb
from xweb.exception import abort
from xweb.globals import response
app = XWeb()
@app.route('/')
def hello():
abort(500)
return 'OK'
@app.exception(500)
def hello():
response.body = "Something wrong"
app.listen(3000)</code></pre>
Test
pip install -r requirement.txt
pytest
How to contribute
- Star
- Fork
- Clone
- Modify
- Test
- Pull request
TODO
important:
- Auto-reload
- Blueprints
normal:
- More test code
- More necessary request arguments
- More http status code
- More necessary middleware