Python开源:Xweb-为 Python 之禅写的极简主义 Web 框架
yosb7306
8年前
<h2 style="text-align:center"><img src="https://simg.open-open.com/show/524943537798692a7e4455f5d6f66282.png"></h2> <h2>Why use xweb</h2> <p>Your life will be long.</p> <ul> <li>Xweb has very simple api.</li> <li>Xweb did not use any third-party dependency.</li> <li>Xweb does not have any redundant code for python2.</li> <li>Source code is easy to understand.</li> <li>It is easy to write xweb's middleware.</li> </ul> <h2>Installation</h2> <p>pip install xweb</p> <h2>Hello world</h2> <pre> <code class="language-python">from xweb.application import XWeb app = XWeb() @app.route('/') def hello(): return 'hello world!' app.listen(3000)</code></pre> <h2>Route</h2> <pre> <code class="language-python">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> <h2>Request</h2> <pre> <code class="language-python">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> <h2>Response</h2> <pre> <code class="language-python">from xweb.globals import response response.headers response.status response.body</code></pre> <h2>Middleware</h2> <pre> <code class="language-python">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> <h2>Exception</h2> <pre> <code class="language-python">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> <h2>Test</h2> <pre> <code class="language-python">pip install -r requirement.txt pytest </code></pre> <h2>How to contribute</h2> <ol> <li>Star</li> <li>Fork</li> <li>Clone</li> <li>Modify</li> <li>Test</li> <li>Pull request</li> </ol> <h2>TODO</h2> <h3>important:</h3> <ol> <li>Auto-reload</li> <li>Blueprints</li> </ol> <h3>normal:</h3> <ol> <li>More test code</li> <li>More necessary request arguments</li> <li>More http status code</li> <li>More necessary middleware</li> </ol> <h2> </h2> <p> </p>