Repository navigation
Expand file tree
/
Copy pathsqlite-ping.py
More file actions
executable file
·127 lines (89 loc) · 2.63 KB
/
Copy pathsqlite-ping.py
File metadata and controls
executable file
·127 lines (89 loc) · 2.63 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Is the website alive? This could be tested at a regular interval by retrieving
a url and checking its status. These tests can be stored in a database which makes
it easy to accumulate statistics and to query for various patterns.
Manual:
https://docs.python.org/3/library/sqlite3.html
The database is named "ping.db" and is stored in the current directory.
If you have the command "sqlite3" installed you could use that to connect to the
database and inspect it.
$ sqlite3 ping.db
"""
import requests
import sqlite3
import time
import sys
def isSQLite3(filename):
"""
Check if a file is a SQLite3 database.
http://stackoverflow.com/questions/12932607/how-to-check-with-python-and-sqlite3-if-one-sqlite-database-file-exists
"""
from os.path import isfile, getsize
if not isfile(filename):
return False
if getsize(filename) < 100: # SQLite database file header is 100 bytes
return False
else:
fd = open(filename, 'rb')
Header = fd.read(100)
fd.close()
if Header[0:16] == b'SQLite format 3\000':
return True
else:
return False
#
# Get argument if there is one and use that as url
#
if len(sys.argv) == 2:
url = sys.argv[1]
else:
sys.stderr.write("Add the url (for example http://bth.se) to check as a command line argument.\n")
sys.stderr.write("{command} http://bth.se.\n\n".format(command=sys.argv[0]))
sys.exit(1)
print(__doc__)
input("Press enter to continue.")
#
# Connect to the database and set a cursor
#
dbFile = "ping.db"
db = sqlite3.connect(dbFile)
dbc = db.cursor()
#
# Init a new database if needed
#
dbc.execute("CREATE TABLE IF NOT EXISTS Pings (url TEXT, time TEXT, response INTEGER)")
#
# Check if the database is valid
#
if not isSQLite3(dbFile):
sys.stderr.write("The file '{}' is not a valid SQLite database.\n".format(dbFile))
sys.exit(1)
#
# get current time for logging
#
rTime = time.ctime(time.time())
try:
# url must start with "http://" or the request fails
print("Connecting to: {}".format(url))
r = requests.head(url, timeout=5)
code = r.status_code
except requests.ConnectionError:
code = 0
#
# insert values into database
#
print("Storing log entry in database")
aTuple = (url, rTime, code)
dbc.execute("INSERT INTO Pings (url, time, response) VALUES (?, ?, ?)", aTuple)
print("Retrieving all rows in database:")
dbc.execute("SELECT url, time, response FROM Pings")
# Loop through a resultset from a SELECT query
for row in dbc:
print(row)
#
# Commit the changes and close the connection
#
db.commit()
db.close()