B. The host "localhost" is invalid and causes the crash.
C. FastAPI app must be run with app.run(), not uvicorn.run().
D. The port number should be an integer, not a string.
Solution
Step 1: Check the port argument type
The port argument must be an integer, but here it is passed as a string "8000" which causes a type error.
Step 2: Verify host and method usage
Host "localhost" is valid, and uvicorn.run is the correct method to start FastAPI apps.
Final Answer:
The port number should be an integer, not a string. -> Option D
Quick Check:
Port must be int, not str [OK]
Hint: Port must be a number, not text [OK]
Common Mistakes:
Passing port as string instead of int
Thinking host "localhost" is invalid
Using wrong method to start app
5. You want your FastAPI app to accept connections only from your local machine and run on port 8080. Which code snippet correctly achieves this and why?
A. Option B, because 127.0.0.1 restricts connections to local machine only on port 8080.
B. Option A, because 0.0.0.0 listens on all interfaces including local machine.
C. Option C, because localhost always means local machine and port 80 is standard.
D. Option D, because 192.168.1.100 is the local IP and port 8080 is correct.
Solution
Step 1: Understand host IP meanings
Host "127.0.0.1" means the app listens only on the local machine (loopback interface), blocking external connections.
Step 2: Check port number
Port 8080 is specified correctly and is a valid port for local testing.
Step 3: Evaluate other options
0.0.0.0 listens on all interfaces, allowing external access; localhost on port 80 uses the wrong port; 192.168.1.100 binds to a specific LAN IP which may not restrict to local machine only.
Final Answer:
Option A, because 127.0.0.1 restricts connections to local machine only on port 8080. -> Option A
Quick Check:
Local only = 127.0.0.1 + port 8080 [OK]
Hint: Use 127.0.0.1 for local-only access [OK]
Common Mistakes:
Choosing 0.0.0.0 for local-only access
Confusing localhost and 127.0.0.1
Using wrong port number
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.