WSGI is a key part of how Flask works. What does WSGI do in a Flask app?
Think about how the web server talks to your Flask code.
WSGI stands for Web Server Gateway Interface. It connects the web server to the Flask app, letting them communicate by passing requests and responses.
Consider this simple Flask app code. What will the browser show when you visit the root URL?
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return 'Hello from WSGI!' if __name__ == '__main__': app.run()
Look at the route and the return value of the function.
The route '/' is linked to the function home(), which returns the string 'Hello from WSGI!'. Flask sends this string as the response body.
Put these steps in the correct order for how WSGI handles a request in Flask.
Think about the flow from client to server and back.
The web server first gets the request, then WSGI passes it to Flask. Flask processes and returns a response, which WSGI sends back through the web server to the client.
Examine this Flask app snippet. What error will it raise?
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return 'Hello WSGI!' if __name__ == '__main__': app.run()
Check the indentation of the function body.
The return statement inside the function is not indented properly, causing an IndentationError.
Look at this Flask app code. When accessed, it returns a 500 Internal Server Error. What is the cause?
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return undefined_variable if __name__ == '__main__': app.run()
Check the return statement inside the route function.
The function tries to return a variable that does not exist, causing a NameError and resulting in a 500 error response.