analitics

Pages

Showing posts with label werkzeug. Show all posts
Showing posts with label werkzeug. Show all posts

Tuesday, July 16, 2019

Python 3.7.3 : Using the werkzeug.

From the official webpage, you can see all the features of this python module:
Werkzeug is a comprehensive WSGI web application library. It began as a simple collection of various utilities for WSGI applications and has become one of the most advanced WSGI utility libraries.
It includes:
An interactive debugger that allows inspecting stack traces and source code in the browser with an interactive interpreter for any frame in the stack.
A full-featured request object with objects to interact with headers, query args, form data, files, and cookies.
A response object that can wrap other WSGI applications and handle streaming data.
A routing system for matching URLs to endpoints and generating URLs for endpoints, with an extensible system for capturing variables from URLs.
HTTP utilities to handle entity tags, cache control, dates, user agents, cookies, files, and more.
A threaded WSGI server for use while developing applications locally.
A test client for simulating HTTP requests during testing without requiring running a server.

The documentation of this python module can be found here.
You can install this python module with the pip tool:
pip install -U Werkzeug
This module can install it if you install the flask python module with python 3.7.3.
Let's test it:
C:\Python373>python
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 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)
...
 * Running on http://localhost:4000/ (Press CTRL+C to quit)
127.0.0.1 - - [16/Jul/2019 13:29:44] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [16/Jul/2019 13:29:46] "GET /favicon.ico HTTP/1.1" 200 -
If you run this source code and open your browser with this http://localhost:4000/ address then you will see a text with Hello, World!.
The common uses of this python package are:
>>> from werkzeug.wrappers import Request, Response
>>> from werkzeug.routing import Map, Rule
>>> from werkzeug.exceptions import HTTPException, NotFound
>>> from werkzeug.wsgi import SharedDataMiddleware
>>> from werkzeug.formparser import parse_form_data
>>> from werkzeug.utils import redirect
>>> from werkzeug.utils import escape
>>> from werkzeug.serving import run_simple
Let's test another simple application with werkzeug python module:
from werkzeug.serving import run_simple
from werkzeug.wrappers import Request
from werkzeug.wrappers import Response

class Application(object):
    def __init__(self):
        print('init Application class')

    def dispatch_request(self, request):
        return Response("Logged in as %s" % request.authorization)

    def __call__(self, environ, start_response):
        request = Request(environ)
        auth = request.authorization
        response = self.dispatch_request(request)
        return response(environ, start_response)

if __name__ == "__main__":
    application = Application()
    run_simple("localhost", 5000, application)
The result will be this:
C:\Python373>python.exe werkzeug_001.py
init Application class
 * Running on http://localhost:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [16/Jul/2019 14:05:31] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [16/Jul/2019 14:05:32] "GET /favicon.ico HTTP/1.1" 200 -
You can add more features to this source code and check with werkzeug features of Web Server Gateway Interface (WSGI).
A full list with examples can be found here.