Webob
Webob is a package for packaging the WSGI request environment and for creating a WSGI response.
Related API link:http://docs.webob.org/en/latest/reference.html
Webob Wsgi Several parameters, the method of return is encapsulated into Reqeust, response these two objects, but also provides a useful exception object, easy to use.
WSGI Server--(WSGI middleware) *-WSGI application
WSGI Application WSGI application is an ordinary callable object, and when a request arrives, WSGI server calls the WSGI app. This object receives two parameters, usually environ,start_response. Environ as described earlier, can be understood as an environment variable, all information related to a request is stored in the environment variable, including server information, client information, request information. Start_response is a callback function, WSGI application returns start_response response to Headers/status server by calling Wsgi. In addition, the Wsgi app will return a iterator object, the iterator is response body. It's so empty to say that it's a lot to see in this simple example. The WSGI middleware middleware exists for the application to have additional behavior. If you are not at liberty to decide whether to put it in front of the program,
It's not middleware anymore.。 But
part of the program。 For example: URL dispatch function, permission to judge.
fromWsgiref.simple_serverImportMake_serverurl_patterns= ( ('hi/','Say_hi'), ('hello/','Say_hello'), )classDispatcher (object):def_match (Self,path): Path= Path.split ('/') [1] forUrl,appinchUrl_patterns:ifPathinchURL:returnappdef __call__(Self,environ, Start_response): Path= Environ.get ('Path_info','/') App=self._match (path)ifApp:app=Globals () [app]returnapp (environ, start_response)Else: Start_response ("404 Not FOUND",[('Content-type','Text/plain')]) return["Page dose not exists!"]defSay_hi (environ, start_response): Start_response ("OK",[('Content-type','text/html')]) return["Kenshin say hi to you!"]defSay_hello (environ, start_response): Start_response ("OK",[('Content-type','text/html')]) return["Kenshin say hello to you!"]app=Dispatcher () httpd= Make_server ("', 8000, App)Print "serving on port 8000 ..."Httpd.serve_forever ()
1.Request
# application/x-www-form-urlencoded: Form data is encoded as a name/value pair. This is the standard encoding format.
# Multipart/form-data: The form data is encoded as a message, and each control on the page corresponds to a part of the message.
# Text/plain: Form data is encoded in plain text with no controls or formatting characters
2.Response
3.Exception
The following summarizes the class definitions for Webob for HTTP return codes.
Exception httpexception Httpok* $- :class: ' Httpok '*201- :class: ' httpcreated '*202- :class: ' httpaccepted '*203- :class: ' Httpnonauthoritativeinformation '*204- :class: ' Httpnocontent '*205- :class: ' Httpresetcontent '*206- :class: ' httppartialcontent ' httpredirection* -- :class: ' Httpmultiplechoices '*301- :class: ' httpmovedpermanently '*302- :class: ' Httpfound '*303- :class: ' Httpseeother '*304- :class: ' httpnotmodified '*305- :class: ' Httpuseproxy '*307- :class: ' Httptemporaryredirect ' Httperror httpclienterror* -- :class: ' Httpbadrequest '*401- :class: ' httpunauthorized '*402- :class: ' httppaymentrequired '*403- :class: ' Httpforbidden '*404- :class: ' Httpnotfound '*405- :class: ' httpmethodnotallowed '*406- :class: ' httpnotacceptable '*407- :class: ' httpproxyauthenticationrequired '*408- :class: ' Httprequesttimeout '*409- :class: ' Httpconflict '*410- :class: ' Httpgone '*411- :class: ' httplengthrequired '*412- :class: ' httppreconditionfailed '*413- :class: ' Httprequestentitytoolarge '*414- :class: ' Httprequesturitoolong '*415- :class: ' Httpunsupportedmediatype '*416- :class: ' httprequestrangenotsatisfiable '*417- :class: ' httpexpectationfailed '*422- :class: ' Httpunprocessableentity '*423- :class: ' httplocked '*424- :class: ' Httpfaileddependency '*428- :class: ' httppreconditionrequired '*429- :class: ' Httptoomanyrequests '*431- :class: ' Httprequestheaderfieldstoolarge '*451- :class: ' httpunavailableforlegalreasons ' Httpservererror* -- :class: ' Httpinternalservererror '*501- :class: ' httpnotimplemented '*502- :class: ' Httpbadgateway '*503- :class: ' httpserviceunavailable '*504- :class: ' Httpgatewaytimeout '*505- :class: ' httpversionnotsupported '*511- :class: ' Httpnetworkauthenticationrequired '
WSGI Learning Series Webob