This repository was archived by the owner on Feb 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_checker.py
More file actions
85 lines (68 loc) · 2.45 KB
/
web_checker.py
File metadata and controls
85 lines (68 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import sqlite3
import requests
DATABASE_FILE = 'db.sqlite'
def setup_database():
"""Setup the SQLite database to store the history of website checks."""
conn = sqlite3.connect(DATABASE_FILE)
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS website_status (
id INTEGER PRIMARY KEY,
url TEXT NOT NULL,
status_code INTEGER,
status_message TEXT
)
''')
conn.commit()
conn.close()
def check_website_status(url):
"""Check the status of the provided URL."""
try:
response = requests.get(url)
return response.status_code, 'UP' if response.status_code == 200 else 'DOWN'
except requests.ConnectionError:
return None, 'DOWN'
def store_website_status(url, status_code, status_message):
"""Store the status of the checked website in the database."""
conn = sqlite3.connect(DATABASE_FILE)
cursor = conn.cursor()
cursor.execute('''
INSERT INTO website_status (url, status_code, status_message)
VALUES (?, ?, ?)
''', (url, status_code, status_message))
conn.commit()
conn.close()
def view_history():
"""View the history of checked websites."""
conn = sqlite3.connect(DATABASE_FILE)
cursor = conn.cursor()
cursor.execute('SELECT url, status_code, status_message FROM website_status ORDER BY id DESC')
rows = cursor.fetchall()
for row in rows:
print(f"URL: {row[0]}, Status Code: {row[1]}, Status: {row[2]}")
conn.close()
def main():
setup_database()
while True:
print("\nWebStatusChecker Menu:")
print("1. Check website status")
print("2. View history")
print("3. Exit")
choice = input("Enter your choice: ")
if choice == '1':
url = input("Enter the website URL: ")
status_code, status_message = check_website_status(url)
if status_code:
print(f"Website {url} is {status_message} (Status Code: {status_code})")
store_website_status(url, status_code, status_message)
else:
print(f"Website {url} is {status_message}")
store_website_status(url, None, status_message)
elif choice == '2':
view_history()
elif choice == '3':
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()