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.
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...
