📜 ⬆️ ⬇️

WSGI, Paste, Pylons - advanced web technologies

Python Logo
Not a single Rails!

Quote from sm.org .
The WSGI theme on Habré is not disclosed, I will try to eliminate the possible illiteracy and just interesting to tell about the current, in my opinion, technology. And at the same time I will touch Paste and gallop - Pylons. But about everything in order.

WSGI


WSGI (Web Server Gateway Interface) is an interface for interaction between a web server and web applications written on it written in Python. WSGI is a 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
The interface was sent as a contender for PEP in the Python Web-SIG on December 7, 2003. And in August 2004, it received its modern name and set sail on a large voyage. A few words about this technology.
The modern variety of programming languages ​​and frameworks for the web certainly horrifies novice programmers. And the more experienced are amazed that even applications written in the same language are highly incompatible and incapable of reusing code. Moving a project from under one server to another often becomes a severe headache. Therefore, the emergence of unifying standards is a logical step in the development of technology.
WSGI is an interface (in other words, a layer) between a web server and a web application . Therefore, from the application side it looks like a web server, and from the server side it looks like an application. This is an important concept. It says that for your project written under WSGI, it doesn’t matter whether it is running on, say, mod_python under Apache or how FastCGI is somewhere else. And you can be engaged in a writing of the application, but not studying of API or an environment in the chosen sheaf.

Hello world!


It looks very good in practice (an example of a WSGI-compatible application):
  <b> def </ b> app (environ, start_response):
   start_response (<span style = "color: # 693"> '200 OK' </ span>, [(<span style = "color: # 693"> 'Content-Type' </ span>, <span style = " color: # 693 "> 'text / plain' </ span>)])
   return [<span style = "color: # 693"> 'Hello World! \ n' </ span>] 

The example is concise and indicative.


Paste comes into play


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

  <b> if </ b> __name__ == <span style = "color: # 693"> '__ main __' </ span>:
   <b> from </ b> paste <b> import </ b> httpserver
   httpserver.serve (app, host = <span style = "color: # 693"> '127.0.0.1' </ span>, port = <span style = "color: # 693"> '8080' </ span>) 

Paste is a metaframe, a set of components and utilities for both the web programmer and the administrator.
Administrator:

Programmer, in turn, Paste

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 development of real-world 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 makes it possible to speak 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, in between).
')

Concept: application stack


Let's go back to the WSGI. Imagine what would happen if within a WSGI application you make preliminary processing of a request (say, by session identifier in cookies, restore a user from the database), call another WSGI application and return the result of the latter? It turns out almost that the scheme to support authentication . And if the first application calls the second, does XSL transformations apply to the results of its activity?
Here is 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. In this way, 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”, AuthKit is a versatile and flexible framework for authorizing users, and not only check . And of course - middleware for catching exceptions and transforming them into “oops” and “oh-oh-oh” pages. It should be noted that Paste encourages wrapping in middleware even such non-Nebov parts as configuration. It turns out a simple, obvious and certainly flexible application structure.

All together and something else - Pylons


So, WSGI provides a web server connection with a multi-layered middleware structure. Paste will help solve typical tasks such as testing, catching exceptions, internal redirects of requests and even the indicator of the file upload (Upload Progress Monitor). Authorization, authentication and work with the database without a single line of SQL were given as an example of middleware just above.
Now we’ll add request routing using Routes , the AJAX backend is WebHelpers (both components are fully ported from RoR), the Mako templates are elegant, with unique capabilities and incredible speed, Unicode and I18N support, let's put it all together into a full-fledged Model-View- Controller structure and get Pylons - an advanced WSGI-compatible web framework for Python, which absorbed all the best from Python, RoR and other frameworks and projects .
In fairness and criticism, it is worth noting that these technologies are very young, for example, Pylons is still in version 0.9.5, and AuthKit is not yet in development and is not 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 perfect (or canonical?) Web framework. Although WSGI technology can be used in TurboGears , CherryPy , Django and other frameworks.
Of course, for at least some objective comparison of them, I don’t 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.

Something Used


Source: https://habr.com/ru/post/11412/


All Articles