Python開源:Xweb-為 Python 之禪寫的極簡主義 Web 框架

yosb7306 7年前發布 | 12K 次閱讀 Python Python開發

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

  1. Star
  2. Fork
  3. Clone
  4. Modify
  5. Test
  6. Pull request

TODO

important:

  1. Auto-reload
  2. Blueprints

normal:

  1. More test code
  2. More necessary request arguments
  3. More http status code
  4. More necessary middleware

 

 

 本文由用戶 yosb7306 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!