Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from fastapi import FastAPI, Request
- from fastapi.responses import HTMLResponse
- import sqlite3
- app = FastAPI()
- # Crear base y tabla si no existen
- conn = sqlite3.connect("data.sqlite")
- cursor = conn.cursor()
- cursor.execute("""
- CREATE TABLE IF NOT EXISTS reportes (
- id INTEGER PRIMARY KEY AUTOINCREMENT,
- user TEXT,
- home TEXT,
- home_size TEXT
- )
- """)
- conn.commit()
- conn.close()
- @app.post("/report")
- async def recibir_reporte(request: Request):
- data = await request.json()
- user = data.get("user", "")
- home = data.get("home", "")
- home_size = data.get("home_size", "")
- conn = sqlite3.connect("data.sqlite")
- cursor = conn.cursor()
- cursor.execute("""
- INSERT INTO reportes (user, home, home_size)
- VALUES (?, ?, ?)
- """, (user, home, home_size))
- conn.commit()
- conn.close()
- return {"mensaje": "Datos guardados en data.sqlite"}
- @app.get("/", response_class=HTMLResponse)
- def ver_reportes():
- conn = sqlite3.connect("data.sqlite")
- cursor = conn.cursor()
- cursor.execute("SELECT user, home, home_size FROM reportes")
- filas = cursor.fetchall()
- conn.close()
- html = """
- <html>
- <head><title>Reportes de usuarios</title></head>
- <body>
- <h2>Reportes guardados</h2>
- <table border="1" cellpadding="5">
- <tr><th>Usuario</th><th>Directorio</th><th>TamaƱo</th></tr>
- """
- for fila in filas:
- html += f"<tr><td>{fila[0]}</td><td>{fila[1]}</td><td>{fila[2]}</td></tr>"
- html += """
- </table>
- </body>
- </html>
- """
- return html
Advertisement
Add Comment
Please, Sign In to add comment