Init Commit

This commit is contained in:
2026-01-17 15:58:06 +03:30
commit b6ad4d18be
6 changed files with 397 additions and 0 deletions

28
Codes/api-redis-json.py Normal file
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/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/mysql_test.py Normal file
View File

@@ -0,0 +1,22 @@
import mysql.connector
# Connect to the database
conn = mysql.connector.connect(
host="localhost",
user="your_user",
password="your_password",
database="your_database"
)
cursor = conn.cursor()
# Run a query
cursor.execute("SELECT * FROM your_table;")
# Fetch and print results
for row in cursor.fetchall():
print(row)
# Close connection
cursor.close()
conn.close()

22
Codes/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