
A Beginner's Guide to Using Werkzeug in Python
Werkzeug is a suite of libraries that can be used to build Web Server Gateway Interface (WSGI) compliant web applications in the Python programming language. At first, Werkzeug was a diverse collection of utilities for WSGI applications, but over time, it has grown into one of the most advanced WSGI utility libraries. Developers are free to specify the template engine, database adapters, and request handling they want. In other words, there is no dependency to follow. Werkzeug also provides features such as debuggers, request and response objects, cache control, cookie handling, file uploading, as well as many additions developed by the community. Werkzeug is issued under BSD license.
Installing:
pip install -U Werkzeug
A WSGI application is an application that can be called by passing an environment dictionary (environment dict) and a start_response function call. The environment contains all the input information, and you can mark the start of the response with the start_response function. These tools allow you to use Request and Response objects, so you don't have to deal with them directly. The Request data takes an environment object and lets you access data from that environment in a suitable way. The Response object itself is a WSGI application that provides a better way to build responses.
Let's look at a code example.
def application(environ, start_response):
request = Request(environ)
text = f"Hello {request.args.get('name', 'World')}!"
response = Response(text, mimetype='text/plain')
return response(environ, start_response)
Struktur dasar:
def __init__(self, config):
self.redis = redis.Redis(
config['redis_host'], config['redis_port'], decode_responses=True
)
def dispatch_request(self, request):
return Response('Hello World!')
def wsgi_app(self, environ, start_response):
request = Request(environ)
response = self.dispatch_request(request)
return response(environ, start_response)
def __call__(self, environ, start_response):
return self.wsgi_app(environ, start_response)
def create_app(redis_host='localhost', redis_port=6379, with_static=True):
app = Shortly({
'redis_host': redis_host,
'redis_port': redis_port
}
if with_static:
app.wsgi_app = SharedDataMiddleware(app.wsgi_app, {
'/static': os.path.join(os.path.dirname(__file__), 'static')
})
return app
if __name__ == '__main__':
from werkzeug.serving import run_simple
app = create_app()
run_simple('127.0.0.1', 5000, app, use_debugger=True, use_reloader=True)
Shortly classes are WSGI applications. __call__ methods are sent to wsgi_app. Then you can wrap the wsgi_app and apply the middleware as you would in the create_app function. The actual wsgi_app method then creates a request object and calls the dispatch_request method. This will return a response object and evaluate it as a WSGI application. Both the short class we create and all request objects in the tool implement the WSGI interface. As a result, you can even return another WSGI application from the dispatch_request method. You can use the create_app function to create a new instance of your application. Add a WSGI middleware that exports a static file and some parameters as configuration to your application as needed, as well as being passed. This way, you can access files from your static folder even if you haven't configured your server to serve them. This is very useful for development.