-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
42 lines (33 loc) · 1.15 KB
/
server.py
File metadata and controls
42 lines (33 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from fastapi import FastAPI
from pydantic import BaseModel
import uvicorn
app = FastAPI()
class Data(BaseModel):
message: str
fruits_db:dict={"desi":["apricot","cherry","apple"],"other":["banana","watemelon"]}
@app.get("/")
def read_root():
return {"message": "Hello from server"}
#This function accepts a key and returns the corresponding value from the fruit database.
def userwant(key):
try:
return fruits_db[key]
except:
return "no data related to the query!"
@app.post("/send/")
def receive_data(data: Data):
userchoice=userwant(data.message)
return {"received": userchoice}
"""
Entry point for deploying the FastAPI application using Uvicorn.
This script runs the FastAPI `app` instance on host 0.0.0.0 and port 8000,
making it accessible externally. It is intended to be used in production or
containerized environments where the application needs to be reachable from
outside the local machine.
Usage:
python server.py
Note:
For advanced deployment (e.g., with Gunicorn), consider using a production-ready ASGI server configuration.
"""
if __name__=="__main__":
uvicorn.run(app=app,host='0.0.0.0',port=8000)