Published on 2024-12-31
Flask Could be Interesting! WebTechWhat exactly are those Gateway Interfaces? The interface dictates how the server should pass incoming requests to the application and how the application should respond to those requests.
Recap All web applications are coupled with HTTP Web Server Software (Apache, etc.) to service to the wider internet users. Please have a look at this post explaining about HTTP Web Server Software for details.
More Recap 😆 A Gateway Interface (GI) is a specification that defines how a web server (e.g., Apache, Nginx, etc.) should communicate with a web application. The interface dictates how the server should pass incoming requests to the application and how the application should respond to those requests. 1. Web Server Software: This is the software that listens for incoming HTTP requests from clients (such as browsers) and sends responses back. Examples include Apache, Nginx, and uWSGI. 2. Web Application: This is the application that processes the requests and generates the responses. This could be a Python web app built using frameworks like Flask, Django, or FastAPI. However, In Node.js, the web application itself acts as the server. There is no need for an intermediary like WSGI or CGI because Node.js uses the HTTP module (or libraries like Express that build on it) to create a server within the application itself, handling requests and responses asynchronously. 3. Gateway Interface: This acts as a middleman between the web server and the web application. It defines how the data (requests and responses) should be passed back and forth.
Since a visual makes all the differences, and also, we are on the last day of 2024, so, why not review the stats to celebrate the end of 2024. Reference is at the end.
HTTP Servers vs WSGI Servers Before the boom of Machine Learning, Python is just a scripting language used to automate the manual tasks. The scripting languages are interpreted language converted into bytecode that is then executed by the Python virtual machine whereas a compiled language (Java, C++, etc.) is transformed into machine understandable language 0110..). Other popular scripting languages are Perl, Visual Basic, JavaScript, Unix Shell Scripts, ECMAScript, and Bash etc.
A traditional web server (HTTP Server Software) does not understand or have any ways to run Python applications. Therefore, Python community came up with Web Server Gateway Interface as a standard interface that modules and containers could implement. WSGI is now the accepted approach for running Python web applications.
Web Server Gateway Interface (WSGI) Flask and Django are WSGI applications, which means they need a WSGI server. WSGI servers have built-in HTTP servers, so they can convert incoming HTTP requests to the standard WSGI environment and then convert outgoing WSGI responses to HTTP responses. But there’s a catch: WSGI can only handle one request at a time. That means each request is handled in a separate thread or process. Not ideal, right? The most popular WSGI servers are gunicorn and uWSGI.
Asynchronous Server Gateway Interface (ASGI) Now, let’s talk about modern Python web application frameworks like FastAPI and Starlette. These frameworks use ASGI instead of WSGI. ASGI was created to fix the limitations of WSGI. It supports protocols beyond HTTP, like WebSockets and GraphQL subscriptions. The most popular ASGI servers are uvicorn, daphne, and hypercorn.
Here’s an example of how ASGI can be used: Imagine a modern web application with WebSocket support, built using FastAPI. That’s exactly what ASGI can do!
Quick note though; many python web frameworks are now ASGI-compatible while still supporting WSGI for backward compatibility (e.g, Django).
Further Reading - Usage of web servers broken down by ranking - FastCGI WhitePaper