import os
import sqlite3
from http.server import SimpleHTTPRequestHandler, HTTPServer
# Define the directory and database file name
db_directory = 'database'
db_filename = 'my_database.db'
db_path = os.path.join(db_directory, db_filename)
# Create or check the SQLite database connection
def check_db_connection():
# Check if the database exists
if not os.path.exists(db_path):
return False
try:
# Try connecting to the database to ensure it's accessible
connection = sqlite3.connect(db_path)
connection.close()
return True
except sqlite3.Error:
return False
# Custom handler to respond to HTTP requests
class MyHandler(SimpleHTTPRequestHandler):
def do_GET(self):
# When accessing the root ("/") page, check if database exists and connection
if self.path == '/':
# Check if the database exists
if not os.path.exists(db_path):
# Create the database if it doesn't exist
self.create_db()
response = "Database created successfully!"
else:
response = "Database already exists."
# Check connection to the database
if check_db_connection():
connection_status = "Database connection successful!"
else:
connection_status = "Failed to connect to the database."
# Respond with both messages
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(f"<html><body><h1>{response}</h1><p>{connection_status}</p></body></html>".encode())
def create_db(self):
# Create the directory if it doesn't exist
if not os.path.exists(db_directory):
os.makedirs(db_directory)
print(f"Directory '{db_directory}' created.")
# Connect to SQLite and create the database file if it doesn't exist
connection = sqlite3.connect(db_path)
cursor = connection.cursor()
# Create a simple table to test the database
cursor.execute('''CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)''')
# Commit changes and close the connection
connection.commit()
cursor.close()
connection.close()
print(f"Database created at '{db_path}'")
# Define the server address and port
server_address = ('', 8080)
# Create and run the server
httpd = HTTPServer(server_address, MyHandler)
print("Server running on port 8080")
httpd.serve_forever()