Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could you explain more detailed differences between mod_wsgi and werkzeug? (SOS newbies)

As I stated on the title, I'm currently feeling pretty uncomfortable of basic understanding of them.

As far as I know, mod_wsgi implemented WSGI specification which can be run under Apache web server.

It was coded in C language.

Another one, werkzeug is a kind of toolkit which have useful utilities. I also reviewed werkzeug can run simple service which is implemented within its sources(make_server in serving.py). I aware that werkzeug has useful features and simple server feature.

What I want to know it the below.

When using Flask like framework based on werkzeug under Apache web server, what does mod_wsgi do exactly?

werkzeug has also basic http server functionality which is don't need to be supported mod_wsgi.

Can anyone explain the differences between mod_wsgi and werkzeug ?

mod_wsgi and werkzeug has duplicated features from the perspective of web server.

like image 633
user1713464 Avatar asked Oct 02 '12 04:10

user1713464


People also ask

What is Werkzeug WSGI?

Werkzeug (WSGI library) is like a communicator between your python code and http nginx/apache server Here is the Complete use case of Werkzeug WSGI: WSGI has two sides: the "server" or "gateway" side (often a web server such as Apache or Nginx), and the "application" or "framework" side (the Python script itself).

How does WSGI work in Python?

WSGI has two sides: the "server" or "gateway" side (often a web server such as Apache or Nginx), and the "application" or "framework" side (the Python script itself). To process a WSGI request, the server side executes the application and provides environment information and a callback function to the application side.

What exactly is Werkzeug?

What exactly is Werkzeug? Bookmark this question. Show activity on this post. Werkzeug is a WSGI utility library for Python. However, when I run my Flask web application, I notice that the response header from the server contains: On the fourth line the server is mentioning a Werkzeug, but what exactly is Werkzeug, is it a web server like Apache?

How does a WSGI request work?

To process a WSGI request, the server side executes the application and provides environment information and a callback function to the application side. The application processes the request, returning the response to the server side using the callback function it was provided.


1 Answers

WSGI stands for Web Server Gateway Interface, (mostly) defined by PEP 333 at http://www.python.org/dev/peps/pep-0333/ .

It is an effort by the Python community to establish a standard mechanism for web servers to speak to Python applications.

In theory, any wsgi compliant server (or extension to an existing web server) should be able to load and run any wsgi compliant application.

werkzeug is a web application framework which can run under a compliant WSGI server, such as Apache+mod_wsgi. It also contains a built-in development server that you can use for development.


WSGI can be very confusing at first, but it is actually pretty simple. The WSGI spec requires that your python application do the following:

  1. define a callable named application
  2. said callable should accept 2 parametes: (environ, start_response)
  3. environ is a dictionary of environment variables
  4. start_response is a callable that needs called to start the response

Once application is called, then it handles the request, builds the output, and:

  1. calls start_response('200 OK', Headers)
  2. return [content]

A simple WSGI app might look like this:

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                    ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

It is strongly suggested that you use an existing WSGI framework, as there are a lot of details involved in parsing HTTP requests, handling file uploads, encoding characters, etc...

Take a look at Bottle, Flask, werkzeug, AppStruct, etc...

like image 194
gahooa Avatar answered Nov 11 '22 18:11

gahooa