Python Web - Werkzeug (WSGI)

> Procedural Languages > Python > Python - Web

1 - About

Werkzeug is a HTTP and WSGI library

request and response objects are provided to work with them.

  • The request data takes the environ object and allows you to access the data from that environ in a nice manner.
  • The response object is a WSGI application in itself and provides a much nicer way to create responses.
Advertising

3 - Example

  • 1 - the Hello World with response object
from werkzeug.wrappers import Response
 
def application(environ, start_response):
    response = Response('Hello World!', mimetype='text/plain')
    return response(environ, start_response)
  • 2 - the Hello World with response object and request object that parses the URL to get the query string 'name'
from werkzeug.wrappers import Request, Response
 
def application(environ, start_response):
    request = Request(environ)
    text = 'Hello %s!' % request.args.get('name', 'World') 
    response = Response(text, mimetype='text/plain')
    return response(environ, start_response)
  • 3 - from the start page
from werkzeug.wrappers import Request, Response
 
@Request.application
def application(request):
    return Response('Hello World!')
 
if __name__ == '__main__':
    from werkzeug.serving import run_simple
    run_simple('localhost', 4000, application)

4 - Features

  • debugger
  • Interactive JavaScript based in-browser debugger
  • Basic session and signed cookie support
  • request and response objects,
  • HTTP header parsing and dumping
  • HTTP utilities to handle entity tags,
  • cache control headers,
  • HTTP dates,
  • cookie handling,
  • file uploads,
  • a URL routing system - routing system for matching URLs to endpoints and vice versa
  • and addon modules.
Advertising