WSGI is a simple way for web servers and Python apps to talk to each other. It helps your Flask app work on the web.
WSGI concept overview in Flask
def application(environ, start_response): status = '200 OK' headers = [('Content-Type', 'text/plain; charset=utf-8')] start_response(status, headers) return [b'Hello, WSGI!']
environ is a dictionary with request info.
start_response is a function to start the HTTP response.
def application(environ, start_response): start_response('200 OK', [('Content-Type', 'text/plain')]) return [b'Hello World']
def application(environ, start_response): path = environ.get('PATH_INFO', '/') if path == '/': start_response('200 OK', [('Content-Type', 'text/html')]) return [b'<h1>Home Page</h1>'] else: start_response('404 Not Found', [('Content-Type', 'text/plain')]) return [b'Page not found']
This program creates a simple WSGI server that listens on port 8000. It shows a welcome message on the home page and a 404 message on other paths.
from wsgiref.simple_server import make_server # Simple WSGI app def application(environ, start_response): path = environ.get('PATH_INFO', '/') if path == '/': start_response('200 OK', [('Content-Type', 'text/html; charset=utf-8')]) return [b'<h1>Welcome to WSGI!</h1>'] else: start_response('404 Not Found', [('Content-Type', 'text/plain; charset=utf-8')]) return [b'Page not found'] # Run server on localhost:8000 with make_server('', 8000, application) as httpd: print('Serving on port 8000...') httpd.serve_forever()
WSGI apps return a list of byte strings as the response body.
Flask uses WSGI behind the scenes to connect your app to the web server.
You usually don't write WSGI apps directly when using Flask, but it's good to understand how it works.
WSGI is the bridge between Python web apps and web servers.
It uses a simple function with two arguments: environ and start_response.
Flask apps run on WSGI servers to handle web requests.
Practice
Solution
Step 1: Understand WSGI's purpose
WSGI stands for Web Server Gateway Interface and connects web servers with Python apps.Step 2: Identify WSGI's role in Flask
Flask apps use WSGI to receive requests from servers and send responses back.Final Answer:
It acts as a bridge between the web server and the Python application. -> Option AQuick Check:
WSGI = bridge between server and app [OK]
- Confusing WSGI with frontend rendering
- Thinking WSGI manages databases
- Assuming WSGI handles user sessions
Solution
Step 1: Recall WSGI callable signature
A WSGI app is a function that takes two arguments: environ and start_response.Step 2: Match the correct function signature
Only def app(environ, start_response): matches the required parameters for a WSGI callable.Final Answer:
def app(environ, start_response): -> Option DQuick Check:
WSGI callable = (environ, start_response) [OK]
- Using request and response instead of environ and start_response
- Missing one or both parameters
- Defining a function with no parameters
def app(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/plain')])
return [b'Hello, WSGI!']Solution
Step 1: Analyze start_response call
The status is '200 OK' and content type is 'text/plain', so plain text will be sent.Step 2: Check the return value
The app returns a list with a byte string containing 'Hello, WSGI!'. This is the response body.Final Answer:
The text 'Hello, WSGI!' displayed in the browser -> Option CQuick Check:
Return byte string with 'Hello, WSGI!' = The text 'Hello, WSGI!' displayed in the browser [OK]
- Returning a string instead of bytes
- Not calling start_response before returning
- Expecting HTML content without setting content-type
def app(environ, start_response):
start_response('200 OK')
return [b'Hello']Solution
Step 1: Check start_response usage
start_response requires two arguments: status and headers list. Here headers are missing.Step 2: Validate return type and function signature
Return type is correct as a list of bytes, and parameters are correct.Final Answer:
Missing the headers argument in start_response call. -> Option BQuick Check:
start_response needs headers list [OK]
- Omitting headers argument in start_response
- Returning string instead of bytes
- Calling start_response after return
Solution
Step 1: Check URL path handling
Use environ['PATH_INFO'] to check the requested path; only '/home' returns 'Welcome!'.Step 2: Verify start_response calls and return values
Both cases call start_response with correct status and headers, and return byte strings in a list.Final Answer:
The code that checks environ['PATH_INFO'] == '/home' and calls start_response in both branches. -> Option AQuick Check:
Check PATH_INFO and call start_response properly [OK]
- Not calling start_response in all branches
- Returning bytes without start_response
- Ignoring PATH_INFO for routing
Ready to prove it?
Pass the test and this pattern moves unknown
