WSGI, Paste, Pylons - Advanced Web Technologies

    Python Logo
    Not a single Rails!

    Quote from sm.org .
    The topic of WSGI on Habré is not disclosed, I will try to eliminate possible illiteracy and it’s just interesting to talk about relevant, in my opinion, technology. And at the same time I will touch upon Paste and gallop - Pylons. But first things first.

    WSGI


    WSGI (Web Server Gateway Interface) - an interface for the interaction of the web server and the web applications running on it written in Python. WSGI is the Python standard and is described in PEP 333 . This is a very young technology. The brainchild of Phillip J. Eby, originally called the Python Web Container
    Interface, was submitted as a contender for PEP in Python Web-SIG on December 7, 2003. And in August 2004 it got its modern name and went on a great voyage. A few words about this technology.
    Modern variety of programming languages ​​and frameworksfor the web, it will surely terrify novice programmers. And the more experienced one is astonishing that even applications written in one language are highly incompatible and incapable of reusing code. Moving a project from one server to another often becomes a big headache. Therefore, the emergence of unifying standards is a logical step in the development of technology.
    WSGI is the interface (in other words, the layer) between the web server and the web application. Therefore, on the application side, it looks like a web server, and on the server side, it looks like an application. This is an important concept. She says that for your project written under WSGI, it is absolutely the same whether it is running, say, on mod_python under Apache or like FastCGI somewhere else. And you can write the application, and not learn the API or the environment in the selected bundle.

    Hello world!


    It looks very tasty in practice (an example of a WSGI-compatible application):
    def app (environ, start_response):
      start_response ( '200 OK' , [( 'Content-Type' , 'text / plain' )])
      return [ 'Hello World! \ n' ]

    The example is concise and indicative.
    • A WSGI application is a callable object (for example, a function) that can be called with two arguments: a dictionary that imitates environment variables, and a callable object to start a response, that is, send an HTTP status (200 OK) and HTTP headers.
    • environ is very similar to a CGI environment and contains familiar values ​​(REQUEST_METHOD, PATH_INFO) along with WSGI-specific ones (environ ['wsgi.input'] contains a stream for reading data transmitted by the POST method).
    • The application returns an iterator with the response body, in other words, an array of strings that will be glued and sent to the browser.


    Paste comes into play


    Want to check how your application works right now by adding 3 lines of code?

    if __name__ == '__main__' :
       from paste import httpserver
      httpserver.serve (app, host = '127.0.0.1' , port = '8080' )

    Paste is a metaframe, a set of components and utilities for both a web programmer and an administrator.
    Admin:
    • It helps to quickly and easily install, run and configure web applications written for Paste; Integrate them with a web server using WSGI (as well as SCGI, FCGI, AJP);
    • will make it easy to maintain, manage and install applications in the system (system-wide).

    To the programmer, in turn, Paste
    • provides command-line utilities and initial templates to greatly simplify the work with your framework;
    • facilitates the entire development cycle: starting a project, updating, testing and deployment,
    • makes the development of WSGI applications a pleasant and interesting thing,
    • and much more .

    In this case, we used the HTTP server built into Paste to instantly show how it all works. This server is also used in the process of developing real applications using frameworks. However, this is a completely standard and necessary thing, also used in Ruby on Rails .
    Paste also contains a lot of useful things . Their presence and abundance just allows us to talk about Paste both as a framework and as a metaframe. The prefix meta says that it is a framework for creating frameworks, and comes from the Greek μετά (after, behind, between).

    Concept: Application Stack


    Back to the WSGI. Imagine what will happen if inside the WSGI application you do the preliminary processing of the request (say, using the session ID in cookies to restore the user from the database), call another WSGI application and return the result of the latter? It turns out almost a scheme to support authentication . And if the first application calls the second, and applies XSL transformations to the results of its activity ?
    Here comes another important concept. The first application in these examples acts as a layer, or middleware. And the totality of all middleware forms the application stack. Thus, a very modular system is obtained in which the components can not even be connected, but simply put in the right order on the WSGI base (without forgetting your application from above).

    Using such a system, you can connect SQLAlchemy - “the Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL”, AuthKit - a universal and flexible framework for authorizing users, and more, check. And of course - middleware for catching exceptions and converting them into the “oops” and “oh-oh-oh” pages. I must say that Paste encourages the wrapping in middleware even of non-Veb parts such as configuration. It turns out a simple, obvious and unconditionally flexible application structure.

    All Together and Something More - Pylons


    So, WSGI provides the connection of a web server with a multilayer middleware structure. Paste will help solve common tasks - testing, catching exceptions, internal redirects of requests, and even a file upload indicator (Upload Progress Monitor). Authorization, authentication and working with a database without a single line of SQL were given as an example of middleware just above.
    Now we add here the routing of requests using Routes , the server side of AJAX is WebHelpers (both components are fully ported with RoR), the Mako templates are elegant, with unique capabilities and incredible speed, support for Unicode and I18N, we will put all this together into a full-fledged Model-View- Controller structure and get Pylons- An advanced WSGI-compatible web framework for Python, which has absorbed the best of Python, RoR and other frameworks and projects .
    In fairness and criticism it’s worth noting that these technologies are very young, for example, Pylons back in version 0.9.5, and AuthKit is in development and is not yet recommended for use in production. Nevertheless, I hope that I managed to convince you that WSGI is promising, and filled with Paste libraries and others is an almost ideal (or canonical?) Web framework. Although you can apply WSGI technology in TurboGears , CherryPy , Django and other frameworks.
    Of course, for at least some objective comparison, I do not have enough experience, but writing the same application under Pylons and Django allows me to conclude that Pylons really steers, although it is more difficult to learn and install.

    Some of the used



    Also popular now: