An Interest In:
Web News this Week
- April 2, 2024
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
A Simple yet Powerful Server with Python
Python is very well known for it's simplicity . Which is a plus point if you are just getting started in the programming world.
In this post, you'll be learning how to create a simple FastAPI server in Python .
FastAPI is really handy to get started with backend development
in python. In this post, you'll see a simple code demo of FastAPI.
So let's get started
Setup Python Environment
Open a directory, open terminal or cmd and write the following command to create a virtual environment with Python v3
$ python3 -v venv env
Activate the Virtual Environment
Windows
$ .\env\Scripts\activate
Linux or Mac
$ . env/bin/activate
Install the Dependencies
Install FastAPI and other dependencies
(env) $ pip install fastapi "uvicorn[standard]"
Code
Create main.py
file
Import FastAPI
from fastapi import FastAPI
Create FastAPI instance,
app
app = FastAPI()
Write a
route
, on which you will be hitting the server (Requesting for someResource
).
@app.get("/")
This is a root route (eg. http://127.0.0.1:8000/)
Now, Write a function, which will be trigerred when you hit this
API end-point
def index(): return {"msg": "Hello, World!"}
Full Code
Combining the above script will result the main.py
a this-
from fastapi import FastAPIapp = FastAPI()@app.get("/")def index(): return {"msg": "Hello, World!"}
Spin up the FastAPI Server
(env) $ uvicorn main:app --reloadINFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)INFO: Started reloader process [28720]INFO: Started server process [28722]INFO: Waiting for application startup.INFO: Application startup complete.
Now, open http://127.0.0.1:8000/ in your browser, you'll se your first server serving the clients.
Explore FastAPI
Now, you have your first server running. You can walk through the FastAPI Docs. and explore the possibilities.
Hurray! You just learned how to setted up A Simple yet Powerful Server with Python .
I hope, you guys liked this quick tutorial. If so, then please don't forget to drop a Like
And also, help me reach 1k Subscribers , on my YouTube channel.
Happy Coding!
Original Link: https://dev.to/rajeshj3/create-a-simple-yet-powerful-server-with-python-2pkp
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To