Flask applications are WSGI (Web Server Gateway Interface) applications. WSGI is essentially a specification that defines how web servers interact with Python based web frameworks like Flask. It acts as a middle layer, standardizing how web servers handle incoming HTTP requests and deliver the Flask application's response.
The WSGI standard v1.0 is specified in PEP 0333. As of September 2010, WSGI v1.0 is superseded by PEP 3333, which defines the v1.0.1 WSGI standard. If you're working with Python 2.x and you're compliant with PEP 0333, then you're also compliant with 3333. The newer version is simply an update for Python 3 and has instructions for how unicode should be handled.
Here is the list of common servers supporting WSGI specification:
Why WSGI specification was created?
The Web Server Gateway Interface (WSGI) specification was created as a standardized interface between web servers and web applications or frameworks for the Python programming language. The creation of WSGI was motivated by several key reasons:
Interoperability: Before WSGI, Python web frameworks and servers had their own unique methods for communication, making it difficult to switch between servers or frameworks without significant modifications to the application. WSGI provided a standardized interface, allowing for greater interoperability between web servers and applications. This means that a WSGI-compliant application can run on any WSGI-compliant web server, and vice versa, without needing to change the application's code.
Simplification: WSGI simplified web application development by defining a simple and universal interface. Developers could focus more on application development rather than on the specifics of server communication. This has led to a more vibrant ecosystem of reusable middleware components and applications.
Flexibility and Reusability: The specification allows for middleware components that can be used to perform functions such as session management, content compression, and URL routing independently of the application's core logic. This promotes the development of reusable components that can be shared among different web applications, enhancing code reuse and reducing development time.
Framework and Server Evolution: By decoupling the application from the server, WSGI has enabled the independent evolution of web frameworks and servers. Developers can choose the best tools for their needs without being locked into a specific server or framework.
Performance Enhancements: WSGI also provided a foundation for optimizing web applications and servers. With a clear interface, it became easier to develop asynchronous servers and applications that could handle a large number of simultaneous connections more efficiently.
In summary, the creation of the WSGI specification was motivated by the need for a standard, flexible, and simple interface that would facilitate interoperability between web servers and applications, encourage the reuse of components, simplify web application development, and support the independent evolution of frameworks and servers.
How WSGI works in Flask
Here's a breakdown of how WSGI works in Flask:
Incoming Request: When a user enters a URL in their browser, the web server receives an HTTP request.
WSGI Interface: The web server translates this request into a format understandable by Flask using the WSGI specification. This format is a dictionary-like object containing details about the request, like the URL path, headers, and any data sent (e.g., from a form).
Flask Application: The WSGI server then passes this dictionary to your Flask application.
Flask Processing: Flask examines the request dictionary and triggers the appropriate function based on the URL path (often called a route). This function generates the response, which can be HTML, data, or any other format.
WSGI Response: Flask returns a data structure following the WSGI specification that represents the response. This structure typically includes status codes, headers, and the response body.
Web Server Response: The WSGI server translates this Flask response structure back into a standard HTTP response and sends it back to the user's browser.
By using WSGI, Flask applications can be deployed on various web servers as long as they have a WSGI compliant interface. This flexibility makes Flask a versatile tool for web development.
Here are some additional points to consider:
Flask itself includes a built-in WSGI development server for testing purposes. This is for development only and not recommended for production due to performance limitations.
In production environments, you'll typically use a dedicated WSGI server like Gunicorn for better performance and scalability.
A common setup involves using a WSGI server behind a reverse proxy server like Nginx. This adds an extra layer of security and can handle functionalities like SSL termination and load balancing.