cleaned structure of codes

This commit is contained in:
2026-01-17 16:47:41 +03:30
parent 89948ac4d7
commit 0ad6b39765
4 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
from fastapi import FastAPI
from pydantic import BaseModel
import redis
# Connect to Redis
r = redis.Redis(host="192.168.6.160", port=30553, db=0, decode_responses=True)
app = FastAPI()
# For sending JSON data
class Item(BaseModel):
key: str
value: str | None = None # make value optional
@app.get("/")
def home():
return {"message": "Redis API is working!"}
# Set key/value using JSON body
@app.post("/set")
def set_json(item: Item):
r.set(item.key, item.value)
return {"message": f"Saved {item.key} = {item.value}"}
@app.post("/get")
def get_json(item: Item):
value = r.get(item.key)
return {"Key": f"{value}"}

26
Codes/Redis/api-redis.py Normal file
View File

@@ -0,0 +1,26 @@
from fastapi import FastAPI
import redis
# connect to redis (make sure redis is running locally)
r = redis.Redis(host="192.168.6.160", port=6379, db=0, decode_responses=True)
app = FastAPI()
@app.get("/")
def home():
return {"message": "Redis API is working!"}
# Set a key/value
@app.post("/set/{key}/{value}")
def set_key(key: str, value: str):
r.set(key, value)
return {"message": f"Key '{key}' set with value '{value}'"}
# Get a key/value
@app.get("/get/{key}")
def get_key(key: str):
value = r.get(key)
if value:
return {"key": key, "value": value}
else:
return {"error": f"Key '{key}' not found"}

22
Codes/Redis/redis_test.py Normal file
View File

@@ -0,0 +1,22 @@
import redis
method=int(input("Enter Method: (1.Read/2.Write): "))
r = redis.Redis(host="192.168.6.160", port=6379,db=0)
if (method == 1):
key=str(input("enter key name: "))
value=r.get(key)
if value == None:
print("Undefined Key")
exit
else:
print(value.decode('utf-8'))
elif (method == 2):
key=str(input("enter key name: "))
value=str(input("enter value: "))
r.set(key,value)
else:
print("Incorrect Input")
exit