#!/usr/bin/env python3
"""
Working web server for tablet management system
"""
from http.server import HTTPServer, BaseHTTPRequestHandler
import sqlite3
from urllib.parse import urlparse, parse_qs
import json
class TabletHandler(BaseHTTPRequestHandler):
def _set_headers(self, status=200, content_type='text/html'):
self.send_response(status)
self.send_header('Content-type', content_type)
self.end_headers()
def do_GET(self):
parsed = urlparse(self.path)
if parsed.path == '/':
self._serve_main_page()
elif parsed.path == '/api/tablets':
self._serve_tablets()
elif parsed.path == '/api/users':
self._serve_users()
elif parsed.path == '/api/loans':
self._serve_loans()
else:
self._set_headers(404)
self.wfile.write(b'404 Not Found')
def _serve_main_page(self):
html = '''
Tablet Management System
Tablet Management System
SQLite backend - Port 8000
System Info
✓ Database: tablets.db
✓ Status: Operational
Port: 8000
'''
self._set_headers()
self.wfile.write(html.encode('utf-8'))
def _serve_tablets(self):
try:
conn = sqlite3.connect('tablets.db')
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
cursor.execute('SELECT * FROM tablets')
tablets = [dict(row) for row in cursor.fetchall()]
conn.close()
self._set_headers(content_type='application/json')
self.wfile.write(json.dumps(tablets).encode('utf-8'))
except Exception as e:
self._set_headers(500)
self.wfile.write(json.dumps({'error': str(e)}).encode('utf-8'))
def _serve_users(self):
try:
conn = sqlite3.connect('tablets.db')
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
cursor.execute('SELECT * FROM users')
users = [dict(row) for row in cursor.fetchall()]
conn.close()
self._set_headers(content_type='application/json')
self.wfile.write(json.dumps(users).encode('utf-8'))
except Exception as e:
self._set_headers(500)
self.wfile.write(json.dumps({'error': str(e)}).encode('utf-8'))
def _serve_loans(self):
try:
conn = sqlite3.connect('tablets.db')
cursor = conn.cursor()
cursor.execute('''
SELECT l.id, l.tablet_id, l.user_id, l.loan_date, l.return_date, l.status,
t.brand || ' ' || t.model as tablet,
u.name as user
FROM loans l
JOIN tablets t ON l.tablet_id = t.id
JOIN users u ON l.user_id = u.id
''')
loans = []
for row in cursor.fetchall():
loans.append({
'id': row[0],
'tablet_id': row[1],
'user_id': row[2],
'loan_date': row[3],
'return_date': row[4],
'status': row[5],
'tablet': row[6],
'user': row[7]
})
conn.close()
self._set_headers(content_type='application/json')
self.wfile.write(json.dumps(loans).encode('utf-8'))
except Exception as e:
self._set_headers(500)
self.wfile.write(json.dumps({'error': str(e)}).encode('utf-8'))
def run_server():
server_address = ('', 8000)
httpd = HTTPServer(server_address, TabletHandler)
print("Tablet Management System - Web Interface")
print("========================================")
print("Server running on: http://localhost:8000")
print("Database: tablets.db (SQLite)")
print("Press Ctrl+C to stop the server")
print()
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nServer stopped")
if __name__ == '__main__':
run_server()