Bird
0
0

Given this WSGI middleware code wrapping a Flask app, what will be printed when a request is made?

medium📝 component behavior Q13 of 15
Flask - Middleware and Extensions
Given this WSGI middleware code wrapping a Flask app, what will be printed when a request is made?
class SimpleMiddleware:
    def __init__(self, app):
        self.app = app
    def __call__(self, environ, start_response):
        print('Before app')
        response = self.app(environ, start_response)
        print('After app')
        return response

app = Flask(__name__)
app = SimpleMiddleware(app)
ANo output is printed
BOnly Before app is printed
COnly After app is printed
DBefore app printed before request, After app printed after request
Step-by-Step Solution
Solution:
  1. Step 1: Analyze middleware call flow

    The middleware prints 'Before app' before calling the wrapped app, then prints 'After app' after the app returns.
  2. Step 2: Understand request handling

    When a request comes, both print statements execute in order around the app call.
  3. Final Answer:

    Before app printed before request, After app printed after request -> Option D
  4. Quick Check:

    Middleware prints before and after app call [OK]
Quick Trick: Middleware __call__ runs code before and after app call [OK]
Common Mistakes:
MISTAKES
  • Assuming only one print runs
  • Thinking prints happen asynchronously
  • Confusing middleware with Flask route handlers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes