Python / Uvicorn Fundamentals Interview Questions
What is the scope dictionary in the ASGI protocol and what does it contain?
The scope dictionary is the first argument passed to an ASGI application callable. It contains metadata about the incoming connection - its type (HTTP, WebSocket, or lifespan) and all relevant connection details.
# Inspecting the scope in an ASGI app async def app(scope, receive, send): print(scope["type"]) # "http", "websocket", or "lifespan" if scope["type"] == "http": print(scope["method"]) # "GET", "POST", etc. print(scope["path"]) # "/api/users" print(scope["query_string"]) # b"page=1&limit=10" print(scope["headers"]) # list of (name, value) byte tuples print(scope["client"]) # ("127.0.0.1", 54321) - IP and port print(scope["server"]) # ("127.0.0.1", 8000) print(scope["scheme"]) # "http" or "https" print(scope["root_path"]) # e.g. "/api/v1" if --root-path is set print(scope["http_version"]) # "1.1" elif scope["type"] == "websocket": print(scope["path"]) # WebSocket path print(scope["headers"]) # Upgrade headers print(scope["subprotocols"]) # Requested sub-protocols elif scope["type"] == "lifespan": pass # handle startup and shutdown events
| Field | Type | Example value |
|---|---|---|
| type | str | "http" |
| method | str | "GET" |
| path | str | '/api/users/42' |
| query_string | bytes | b'page=1&limit=10' |
| headers | list of tuples | [(b'content-type', b'application/json')] |
| client | tuple or None | ('127.0.0.1', 54321) |
| scheme | str | "https" |
| root_path | str | '/api/v1' |
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
