About Python bottle

Source: Internet
Author: User
Tags what web server

Bottle is a lightweight python Web Framework, can be adapted to a variety of Web Server, including Python's own wsgiref (default), Gevent, Cherrypy,gunicorn, and so on. Bottle is a single-file release, the source code can be downloaded here, the volume is not much, can be used to learn the web framework.  There are also Chinese translations of official documents. First, let's run bottle's Hello world.
From bottle import Run if __name__ = = ' __main__ ':    def application (environ, start_response):        start_response (' 200 OK ', [(' Content-type ', ' text/html ')])        return [' 
The above code also looks very much in line with the WSGI interface specification. Start the change code and you can see the output Bottle V0.13-dev server starting up (usingWsgirefserver())...  Listening on Http://localhost:8080/Hit ctrl-c to quit. The bold portion of the output indicates that the Web server being used is a python-wsgiref. You can also use other Web servers, such as gevent, if you need to install gevent, and the modified code is as follows:
From bottle import Runimport gevent.monkeygevent.monkey.patch_all () if __name__ = = ' __main__ ':    def application ( Environ, start_response):        start_response (' K OK ', [(' Content-type ', ' text/html ')])        return [' 

By specifying the Web server as ' gevent ' with the server keyword, the first line of the output becomes:

Bottle V0.13-dev Server starting up (usingGeventserver())... Regardless of what Web server bottle uses to boot, in the browser input 127.0.0.1:8080, you can see the following bottle the central classification and interfacebottle. BottleRepresents an independent WSGI application, made up of parts: routes, callbacks, plugins, resources and configuration. __CALL__: Bottle defines the __call__ function so that the bottle instance can be a callable. As mentioned in the previous article, the Web framework (or application) needs to provide a Callbale object to the Web server, bottle provides the bottle instance
    def __call__ (self, Environ, start_response): "" and each    instance of:class: ' Bottle ' is a WSGI application.       "" Return Self.wsgi (environ, start_response)        
The following is the core code of the BOTTLE.WSGI function, which mainly calls two more important functions: _handle, _cast
    def wsgi (self, Environ, start_response): "" "the        bottle wsgi-interface. "" "        try: Out            = Self._cast (Self._handle (environ))            # rfc2616 sections 4.3            if response._status_code in ( 101, 204, 304)            or environ[' request_method '] = = ' HEAD ':                if hasattr (out, ' close '): Out.close () out                = []
   start_response (Response._status_line, response.headerlist)            return out

_handle: Process the request, final call to application, the simplified code is as follows:

1 def _handle (self, environ): 2         self.trigger_hook (' Before_request ') 3         route, args = Self.router.match ( Environ) 4 out         = Route.call (**args) 5         self.trigger_hook (' after_request ') 6         return out
_cast: The standard WSGI interface requires strict return values for application, and the return string must be iterated. Bottle has made some extensions that allow the app to return richer types, such as Dict,file. The _cast function processes the return value of the _handle function to conform to the WSGI specification bottle. RouteEncapsulates routing rules and corresponding callbacksbottle. Router
A Router is an ordered collection of route->target pairs. It is used to efficiently match WSGI requests against a number of routes and return the first target that satisfies the R Equest.
ServeradapterAll bottle the base class of the Web server, subclasses can only implement the Run method, bottle there are a large number of Web server adaptation. The following table is from the official website, which describes the various Web servers supported by bottle, as well as their respective features.
Name Homepage Description
Cgi Run as CGI script
Flup Flup Run as FastCGI process
Gae Gae Helper for Google App Engine deployments
Wsgiref Wsgiref single-threaded Default Server
CherryPy CherryPy Multi-threaded and very stable
Paste Paste Multi-threaded, stable, tried and tested
Rocket Rocket Multi-threaded
Waitress Waitress Multi-threaded, Poweres Pyramid
Gunicorn Gunicorn Pre-forked, partly written in C
Eventlet Eventlet Asynchronous framework with WSGI support.
Gevent Gevent Asynchronous (Greenlets)
Diesel Diesel Asynchronous (Greenlets)
Fapws3 Fapws3 Asynchronous (network side only), written in C
Tornado Tornado Asynchronous, powers some parts of Facebook
Twisted Twisted asynchronous, well tested ... twisted
Meinheld Meinheld Asynchronous, partly written in C
Bjoern Bjoern Asynchronous, very fast and written in C
Auto Automatically selects an available server adapter

As you can see, bottle is a rich Web server. The working mode is also comprehensive, with multi-threaded (such as paste), multi-process mode (such as Gunicorn), and also based on co-processes (such as gevent). The specific choice of Web server depends on the characteristics of the application, such as CPU bound or IO boundBottle.runStart the WSGI server. Several of the more important parameters are APP:WSGI application, which can be bottle. Bottle is also starting to be any function that satisfies the WSGI interface Server:wsgi HTTP Server, string Host:port: Listening Port core logic: Serveradapter.run (APP). Finally, there are some examples of using descriptor in the bottle source code, the implementation is very ingenious, it is worth reading, the previous article also introduced. references;http://www.bottlepy.org/docs/dev/https://raw.githubusercontent.com/bottlepy/bottle/master/ bottle.pyhttp://blog.csdn.net/huithe/article/details/8087645http://simple-is-better.com/news/59http:// www.bottlepy.org/docs/dev/deployment.html#server-optionshttp://blog.rutwick.com/ Use-bottle-python-framework-with-google-app-engine

About Python bottle

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.