Cybrkyd's Git Repositories

python-budget - commit: 4a1eefc

commit 4a1eefc3f0379de3c14212c156a530ee98d88a0ec47610d5df8fb3d5ce293f5d
author Cybrkyd <git@cybrkyd.com> 2026-05-28 18:44:43 +0100
committer Cybrkyd <git@cybrkyd.com> 2026-05-28 18:44:43 +0100

Commit Message

Silence the tracebacks from serve_forever() on exit.

📊 Diffstat

budget.py 20
1 files changed, 12 insertions(+), 8 deletions(-)

Diff

diff --git a/budget.py b/budget.py
index 5020da2..b7fca74 100644
--- a/budget.py
+++ b/budget.py
@@ -5,7 +5,6 @@ from datetime import datetime
DB = "budget.db"
-
def init_db():
with sqlite3.connect(DB) as conn:
conn.execute("""
@@ -16,13 +15,11 @@ def init_db():
)
""")
-
def get_balance():
with sqlite3.connect(DB) as conn:
cur = conn.execute("SELECT COALESCE(SUM(amount), 0) FROM entries")
return cur.fetchone()[0]
-
def get_month_total():
now = datetime.now()
start = datetime(now.year, now.month, 1)
@@ -36,7 +33,6 @@ def get_month_total():
""", (start.isoformat(), end.isoformat()))
return cur.fetchone()[0]
-
HTML = """
<!doctype html>
<html>
@@ -63,7 +59,6 @@ HTML = """
</html>
"""
-
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path != "/":
@@ -101,9 +96,18 @@ class Handler(BaseHTTPRequestHandler):
self.send_header("Location", "/")
self.end_headers()
-
- if __name__ == "__main__":
+ def run():
init_db()
+ server = HTTPServer(("localhost", 8080), Handler)
print("Running on http://localhost:8080")
- HTTPServer(("localhost", 8080), Handler).serve_forever()
+
+ try:
+ server.serve_forever()
+ except KeyboardInterrupt:
+ pass
+ finally:
+ server.server_close()
+
+ if __name__ == "__main__":
+ run()