Skip to content

Instantly share code, notes, and snippets.

@KarimullinArthur
Created July 24, 2023 14:48
Show Gist options
  • Save KarimullinArthur/18b35de2a9643c9dec55e0acc3ce90ff to your computer and use it in GitHub Desktop.
Save KarimullinArthur/18b35de2a9643c9dec55e0acc3ce90ff to your computer and use it in GitHub Desktop.
learn FastApi. Api send disk space info, update with cron.
# import uvicorn
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=8000, log_level="info")
from typing import Union
import json
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
with open('./space.txt') as file:
data = json.loads(file.read())
result = {
"datetime": data['datetime'],
"total_used_gb": data['total'],
"free_used_gb": data['free'],
"percent_used": data['percent']}
return result
@app.get("/items/{item_id}")
def read_item(item_id: int, q: Union[str, None] = None):
return {"item_id": item_id, "q": q}
import requests
resp = requests.get('http://127.0.0.1:8000')
print(resp, '\n')
print(type(resp.text), resp.text, '\n')
print(type(resp.json()), resp.json(), '\n')
print('---')
resp = requests.get('http://46.229.212.49:8000')
print(resp, '\n')
print(type(resp.text), resp.text, '\n')
print(type(resp.json()), resp.json(), '\n')
DISK_SIZE_TOTAL=$(df -kh . | tail -n1 | awk '{print $2}')
DISK_SIZE_FREE=$(df -kh . | tail -n1 | awk '{print $4}')
DISK_PERCENT_USED=$(df -kh . | tail -n1 | awk '{print $5}')
DATETIME=$(date -uIseconds)
echo '{
"datetime": "'$DATETIME'",
"total": "'$DISK_SIZE_TOTAL'",
"free": "'$DISK_SIZE_FREE'",
"percent": "'$DISK_PERCENT_USED'"
}' > /home/arthur/learn/python/fastapi/space.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment