




A collection of WSGI packages
See: example.py
The error handler is a WSGI app which calls another WSGI app.
If that WSGI app raises awsgi_tools.error.HTTPException
, the error code and an optional message will be returńed.
If that WSGI app raises a normal Exception, the error code500
will be returńed.
Import:
fromwsgi_tools.errorimportErrorHandler,JSONErrorHandler,HTMLErrorHandler
To use theErrorHandler
you have to overwrite the abstracthandle
method or use the prebuildJSONErrorHandler
orHTMLErrorHandler
.
app=JSONErrorHandler(app0)
With this you can serve easy-to-use functions over WSGI.
defapp0(request):data=request.body_jsonresponse=do_something(data)return200,response
The router is a WSGI app which reads the path of the request and calls another corresponding WSGI app.
Import:
fromwsgi_tools.routingimportRouter
Create the Router:
The first argument ofRouter
is thelist
of rules you want to use.
A rule an instance of an subclass ofwsgi_tools.routing.Rule
.
The order of these rules is important, because you don't want to throw an405 Method Not Allowed
error if there are no endpoints which match one of the endpoint and the method.
So the path-checking rule must be before the method checking rule.
There are premade rules for matching path, method and content-type:
fromwsgi_tools.routingimportPathRule,METHOD_RULE,CONTENT_TYPE_RULE
The second argument is thedict
with tuples as keys, which represent the args for the rules and wsgi apps as keys.
fromwsgi_tools.routingimportRouter,PathRule,METHOD_RULE,CONTENT_TYPE_RULEpath_rule=PathRule()app=Router( [path_rule,METHOD_RULE,CONTENT_TYPE_RULE], { (('/create',),'POST','json'):create_app, (('/',int,'/options'),'GET',None):options_app })
If you send aPOST
request to/create
and the content-type is*/json
,*/*+json
,*/json+*
or*/*+json+*
,create_app
will be called.
If you send aGET
request to/3/options
,options_app
will be called andpath_rule.args
will be[3]
.