sergio_educacionit

main.py

Apr 21st, 2026 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.12 KB | None | 0 0
  1. import sqlite3
  2. from fastapi import FastAPI
  3. from fastapi.responses import HTMLResponse
  4. from pydantic import BaseModel
  5.  
  6.  
  7. app = FastAPI()
  8.  
  9. DB_FILE = "db.sqlite"
  10.  
  11. class ServerData(BaseModel):
  12.     static_hostname: str
  13.     icon_name: str
  14.     chassis: str
  15.     machine_id: str
  16.     boot_id: str
  17.     virtualization: str
  18.     operating_system: str
  19.     kernel: str
  20.     architecture: str
  21.     hardware_vendor: str
  22.     hardware_model: str
  23.     firmware_version: str
  24.     firmware_date: str
  25.     firmware_age: str
  26.  
  27.  
  28.  
  29. def init_db():
  30.     conn = sqlite3.connect(DB_FILE)
  31.     cursor = conn.cursor()
  32.  
  33.     cursor.execute("""
  34.        CREATE TABLE IF NOT EXISTS servers (
  35.            id INTEGER PRIMARY KEY AUTOINCREMENT,
  36.            static_hostname TEXT,
  37.            icon_name TEXT,
  38.            chassis TEXT,
  39.            machine_id TEXT UNIQUE,
  40.            boot_id TEXT,
  41.            virtualization TEXT,
  42.            operating_system TEXT,
  43.            kernel TEXT,
  44.            architecture TEXT,
  45.            hardware_vendor TEXT,
  46.            hardware_model TEXT,
  47.            firmware_version TEXT,
  48.            firmware_date TEXT,
  49.            firmware_age TEXT
  50.        )
  51.    """)
  52.  
  53.     conn.commit()
  54.     conn.close()
  55.  
  56. @app.on_event("startup")
  57. def startup():
  58.     init_db()
  59.  
  60.  
  61.  
  62. @app.get("/", response_class=HTMLResponse)
  63. def root():
  64.     conn = sqlite3.connect(DB_FILE)
  65.     cursor = conn.cursor()
  66.  
  67.     cursor.execute("SELECT * FROM servers")
  68.     rows = cursor.fetchall()
  69.  
  70.     conn.close()
  71.  
  72.     html = "<h1>Servers List</h1><table border='1'>"
  73.     html += "<tr><th>id</th><th>static_hostname</th><th>icon_name</th><th>chassis</th><th>machine_id</th><th>boot_id</th><th>virtualization</th><th>operating_system</th><th>kernel</th><th>architecture</th><th>hardware_vendor</th><th>hardware_model</th><th>firmware_version</th><th>firmware_date</th><th>firmware_age</th></tr>"
  74.  
  75.     for row in rows:
  76.         html += "<tr>" + "".join(f"<td>{col}</td>" for col in row) + "</tr>"
  77.  
  78.     html += "</table>"
  79.  
  80.     return html
  81.  
  82.  
  83. @app.post("/servers")
  84. def save_server(server: ServerData):
  85.     conn = sqlite3.connect(DB_FILE)
  86.     cursor = conn.cursor()
  87.  
  88.     cursor.execute("""
  89.        INSERT INTO servers (
  90.            static_hostname,
  91.            icon_name,
  92.            chassis,
  93.            machine_id,
  94.            boot_id,
  95.            virtualization,
  96.            operating_system,
  97.            kernel,
  98.            architecture,
  99.            hardware_vendor,
  100.            hardware_model,
  101.            firmware_version,
  102.            firmware_date,
  103.            firmware_age
  104.        ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  105.    """, (
  106.         server.static_hostname,
  107.         server.icon_name,
  108.         server.chassis,
  109.         server.machine_id,
  110.         server.boot_id,
  111.         server.virtualization,
  112.         server.operating_system,
  113.         server.kernel,
  114.         server.architecture,
  115.         server.hardware_vendor,
  116.         server.hardware_model,
  117.         server.firmware_version,
  118.         server.firmware_date,
  119.         server.firmware_age
  120.     ))
  121.  
  122.     conn.commit()
  123.     conn.close()
  124.  
  125.     return {"message": "saved"}
  126.  
Advertisement
Add Comment
Please, Sign In to add comment