Junior — Middle
Explain what a WSGI interface is and how it is used in Python web applications.
sobes.tech AI
Answer from AI
WSGI (Web Server Gateway Interface) is a standard interface between web servers and web applications or frameworks in Python. It defines how the server forwards requests to the application and how the application returns responses.
WSGI allows separating the web server from the application logic, ensuring compatibility between different servers and frameworks.
Example of a simple WSGI application:
def application(environ, start_response):
status = '200 OK'
headers = [('Content-type', 'text/plain; charset=utf-8')]
start_response(status, headers)
return [b"Hello, WSGI!"]
Here, environ is a dictionary with request information, start_response is a function to start the response. The web server calls application, receives the response, and sends it to the client.
WSGI is used in many popular frameworks (e.g., Flask, Django) and servers (Gunicorn, uWSGI).