A simple web framework for Python.
Project description
miniweb
A simple web framework for Python.
Example 1. Working with gunicorn.
/path/to/your/project/debugserver.py
from miniweb import Application
from miniweb import simplejson_api
@simplejson_api
def ping(http_request, http_response):
return "pong"
@simplejson_api
def echo(http_request, http_response):
return http_request.GET.get("msg", "")
application = Application()
application.router.add_route("/ping", ping)
application.router.add_route("/echo", echo)
/path/to/your/project/wsgi.conf.py
bind = ["0.0.0.0:9132"]
workers = 4
threads = 32
daemon = True
errorlog = "logs/gunicorn.error.log"
keepalive = 300
timeout = 300
graceful_timeout = 300
loglevel = "info"
start.sh
#!/bin/bash
cd /path/to/your/project/
gunicorn --config=wsgi.conf.py --pidfile=/path/to/your/project/gunicorn.pid debugserver:application
Test results:
In [14]: import requests
In [15]: requests.get('http://127.0.0.1:9132/ping').json()
Out[15]: {'success': True, 'result': 'pong', 'error': {'code': 0, 'message': 'OK'}}
In [16]: requests.get('http://127.0.0.1:9132/echo?msg=hello').json()
Out[16]: {'success': True, 'result': 'hello', 'error': {'code': 0, 'message': 'OK'}}
Example 2. Working with gevent.pywsgi.
/path/to/your/project/debugserver.py
- Server code is the same with the server code using gunicorn.
start.sh
#!/bin/bash
cd /path/to/your/project/
python -m gevent.pywsgi -b 0.0.0.0:9132 debugserver:application
Test results:
In [14]: import requests
In [15]: requests.get('http://127.0.0.1:9132/ping').json()
Out[15]: {'success': True, 'result': 'pong', 'error': {'code': 0, 'message': 'OK'}}
In [16]: requests.get('http://127.0.0.1:9132/echo?msg=hello').json()
Out[16]: {'success': True, 'result': 'hello', 'error': {'code': 0, 'message': 'OK'}}
Example 3. Working with miniweb.ThreadingWSGIServer.
debugserver.py
from miniweb import ThreadingWSGIServer
from miniweb import Application
from miniweb import simplejson_api
@simplejson_api
def ping(http_request, http_response):
return "pong"
@simplejson_api
def echo(http_request, http_response):
return http_request.GET.get("msg", "")
def main():
app = Application()
app.router.add_route("/ping", ping)
app.router.add_route("/echo", echo)
server = ThreadingWSGIServer(app, listen="127.0.0.1", port=9132)
server.serve_forever()
if __name__ == "__main__":
main()
Start debugserver with command python3 debugserver.py
, and then use ipython to do requests.
In [1]: import requests
In [2]: requests.get('http://127.0.0.1:9132/ping').content
Out[2]: b'{"success": true, "result": "pong", "error": {"code": 0, "message": "OK"}}'
In [3]: requests.get('http://127.0.0.1:9132/echo?msg=hi').content
Out[3]: b'{"success": true, "result": "hi", "error": {"code": 0, "message": "OK"}}'
How to write a request handler?
def ping(http_request:HttpRequest, http_resposne:HttpResponse) -> None:
http_resposne.response("pong")
- A handle is a callable object and always takes two parameters: http_request and http_resposne.
- The parameter http_request holds all information about the request, e.g. META, GET, POST, COOKIES and FILES...
- The parameter http_resposne is used to handle all response things, e.g. status_code, response_content, response_headers, response_cookies...
- The handler returns nothing, and all things returned will be discarded, all response things should done by http_response methods.
How to use Middlewares?
By default the wsgi server allows all request methods and doesn't known how to handler OPTIONS request properly. With OptionsHandlerMiddleware, the wsgi server can handle the OPTIONS request and returns allowed methods to client.
from miniweb import Application
from miniweb.server import ThreadingWSGIServer
from miniweb.middlewares import OptionsHandlerMiddleware
from miniweb.sapi import simplejson_api
@simplejson_api
def ping(http_request, http_response):
return "pong"
@simplejson_api
def echo(http_request, http_response):
return http_request.POST.get("msg", "")
echo.allowed_methods = ["POST"]
application = Application()
application.router.add_route("/ping", ping)
application.router.add_route("/echo", echo)
application.middlewares = [
OptionsHandlerMiddleware,
]
server = ThreadingWSGIServer(application, listen="0.0.0.0", port=9132)
server.serve_forever()
Test results:
In [1]: import requests
In [2]: requests.get('http://127.0.0.1:9132/ping').content
Out[2]: b'{"success": true, "result": "pong", "error": {"code": 0, "message": "OK"}}'
In [3]: requests.post('http://127.0.0.1:9132/echo', data={"msg": "hi"}).content
Out[3]: b'{"success": true, "result": "hi", "error": {"code": 0, "message": "OK"}}'
In [4]: requests.options('http://127.0.0.1:9132/ping').headers
Out[4]: {'Date': 'Thu, 03 Mar 2022 08:17:09 GMT', 'Server': 'WSGIServer/0.2 CPython/3.7.9', 'Allow': 'OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT', 'Content-Type': 'text/plain', 'Content-Length': '0'}
In [5]: requests.options('http://127.0.0.1:9132/echo').headers
Out[5]: {'Date': 'Thu, 03 Mar 2022 08:17:18 GMT', 'Server': 'WSGIServer/0.2 CPython/3.7.9', 'Allow': 'OPTIONS, POST', 'Content-Type': 'text/plain', 'Content-Length': '0'}
What is SAPIs (Simple APIs)?
- miniweb.sapi decorators help you make a json or jsonp response easily.
- Instead of set response content by http_response methods, with miniweb.sapi you just returns response data from the handler function, and the SAPIs decorator will call http_response methods for your. For example:
# ################################################################### # Inside the handle we just returns the core result "pong", # but simplejson_api will do the result pack for you, # so that you get the final result: # { # "success": True, # "result": "pong", # "error": { # "code": 0, # "message": "OK", # } # } # ################################################################### @simplejson_api def ping(http_request:HttpRequest, http_resposne:HttpResponse): return "pong"
Releases
v0.1.5
- First release.
- Core dispatch and router dispatch are ready.
- HttpRequest and HttpResponse are ready.
- SimpleAPI decorators are eady.
@TODO:Done in v0.1.7.multipart/form-data
content type is NOT supported yet.
v0.1.6
- Fix HttpResponse init problem in core.dispatch.
- Use ensure_ascii=False while doing json.dumps in miniweb.sapi.
- Add HttpRequest.content_type and HttpRequest.content_length.
v0.1.7
- Handler PayloadTooLarge exception.
- Handler LengthRequired exception.
- Add
multipart/form-data
content type support. - Add file upload support.
v0.1.8
- Add response file support.
- Add miniweb.contrib.static_files utils.
- Add router name support.
- Add router reverse by the name support.
- Fix HttpRequest.update_post_data problem.
- Accept ALL request methods by default.
v0.1.9
- Add LengthRequired exception handler.
- Add HttpChunkResponseData response data type and it's handler.
- Fixed the problem that caused errors when referencing http_request.POST data in GET requests.
v0.1.10
- Fix problem in python3.7.
v0.1.11
- Add ThreadingWSGIServer.
v0.1.12
- Fix wsgi_input.read blocked problem.
- Add miniweb.middlewares.OptionsHandlerMiddleware.
- Rename application's global_permitted_methods to global_allowed_methods, to match with the standard header Allow.
- Set application's default allowed methods to ["OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE", "TRACE", "CONNECT"].
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file miniweb-0.1.12.tar.gz
.
File metadata
- Download URL: miniweb-0.1.12.tar.gz
- Upload date:
- Size: 15.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.7.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7545d40794506fe6593ddd318dbe2d5beebe22222c6f3c62f586547e775018da |
|
MD5 | 1d0f1752513c060388d1a658c8ec913c |
|
BLAKE2b-256 | 3f5d9a47b98d6065eb3511630e1b4ed2a0b9c2e9fd6947f3b21b5a6f03e350bd |
File details
Details for the file miniweb-0.1.12-py3-none-any.whl
.
File metadata
- Download URL: miniweb-0.1.12-py3-none-any.whl
- Upload date:
- Size: 15.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.7.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9030f992e6a422bc3c2ef933a941d252792f4a9e422714eb2cfee57b1a7b0074 |
|
MD5 | 34b5162dc6b33464e1c6685a3726e01a |
|
BLAKE2b-256 | 32ecaf6515da1424a9b2d2acfcc257f38057c025cd98e5d0d292ca7a4068da30 |