Pages

Friday, 10 January 2020

Python and Web --- (3) WSGI helper: Werkzeug


Writing web applications directly interfacing with WSGI/mod_wsgi is kind of boring. So libraries have been developed to help ease this process. Among them, "werkzeug" is the most popular one.

1. werkzeug is a WSGI lib


# cat app.wsgi
from werkzeug.wrappers import Response
def application(environ, start_response):
        response = Response('Hello World!', mimetype='text/plain')
        return response(environ, start_response)

A little better, right?

2. werkzeug provides a simpe WSGI Server

# cat app.wsgi

from werkzeug.wrappers import Response
def application(environ, start_response):
        response = Response('Hello World!', mimetype='text/plain')
        return response(environ, start_response)

if __name__ == '__main__':
        from werkzeug.serving import run_simple
        run_simple('127.0.0.1', 80, application)

This simple WSGI server app is convenient when developing web applications. You don't have to start Apache/mod_wsgi any more on your development computer.

No comments:

Post a Comment