Skip to main content

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

After debugserver start, start ipython and do requests

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

After debugserver start, start ipython and do requests

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'}}

How to write a request handler?

def ping(http_request:HttpRequest, http_resposne:HttpResponse) -> None:
    http_resposne.response("pong")
  1. A handle is a callable object and always takes two parameters: http_request and http_resposne.
  2. The parameter http_request holds all information about the request, e.g. META, GET, POST, COOKIES and FILES...
  3. The parameter http_resposne is used to handle all response things, e.g. status_code, response_content, response_headers, response_cookies...
  4. The handler returns nothing, and all things returned will be discarded, all response things should done by http_response methods.

What is SAPIs (Simple APIs)?

  1. miniweb.sapi decorators help you make a json or jsonp response easily.
  2. 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: multipart/form-data content type is NOT supported yet. Done in v0.1.7.

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

miniweb-0.1.7.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

miniweb-0.1.7-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file miniweb-0.1.7.tar.gz.

File metadata

  • Download URL: miniweb-0.1.7.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10

File hashes

Hashes for miniweb-0.1.7.tar.gz
Algorithm Hash digest
SHA256 70ab0f4751a8e2e7db2cd0dcbf82a1dc6c39590bfba22d41a8d415ac5a7d75e5
MD5 740320eb7803bb531c030a71e336fc4f
BLAKE2b-256 7a3ae46eacb42e1bb45b8c540078fbe59267d3d705c111fd4a415fb49c080480

See more details on using hashes here.

File details

Details for the file miniweb-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: miniweb-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10

File hashes

Hashes for miniweb-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 7ac5c963397372d2ccffe512f72e9b0af6b73511802e9d42a22daa15695cedd7
MD5 733abad522063563bc029ab89a551ef5
BLAKE2b-256 f69fe9574f6cf6270171f1f6b5351653e4ad55db2a9cc4e17b5132c017639958

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page