Python / Uvicorn Fundamentals Interview Questions
How do you run a basic Uvicorn server from the command line?
Uvicorn is started from the command line by specifying the application in module:attribute format. The module is the Python file (without .py), and the attribute is the ASGI application object inside it.
# File: main.py async def app(scope, receive, send): assert scope['type'] == 'http' await send({ 'type': 'http.response.start', 'status': 200, 'headers': [(b'content-type', b'text/plain')], }) await send({ 'type': 'http.response.body', 'body': b'Hello, world!', }) # Run from the terminal: # uvicorn main:app
# Basic run - listens on 127.0.0.1:8000 uvicorn main:app # Custom host and port uvicorn main:app --host 0.0.0.0 --port 8080 # Development mode with auto-reload uvicorn main:app --reload # FastAPI example # File: api.py from fastapi import FastAPI app = FastAPI() @app.get("/") def read_root(): return {"Hello": "World"} # Run FastAPI app uvicorn api:app --reload # Nested attribute path # File: mypackage/server.py with app = FastAPI() uvicorn mypackage.server:app
The application path follows Python import syntax: dots separate packages, the colon separates the module from the attribute. If your ASGI application lives at mypackage/server.py and is named app, you write mypackage.server:app.
More Related questions...