Skip to content

Commit 316d730

Browse files
committed
removing database and updated before first release
1 parent b7b83ac commit 316d730

2 files changed

Lines changed: 98 additions & 26 deletions

File tree

‎example/sqlite/ping.sqlite3‎

-3 KB
Binary file not shown.

‎example/sqlite/sqlite-ping.py‎

Lines changed: 98 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,126 @@
11
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
24
"""
3-
Test example to take a ping and save it into an sqlite3 database
4-
https://docs.python.org/3/library/sqlite3.html
5+
Is the website alive? This could be tested at a regular interval by retrieving
6+
a url and checking its status. These tests can be stored in a database which makes
7+
it easy to accumulate statistics and to query for various patterns.
8+
9+
Manual:
10+
https://docs.python.org/3/library/sqlite3.html
11+
12+
The database is named "ping.db" and is stored in the current directory.
13+
14+
If you have the command "sqlite3" installed you could use that to connect to the
15+
database and inspect it.
16+
17+
$ sqlite3 ping.db
18+
519
"""
6-
# pip install requests
20+
721
import requests
822
import sqlite3
923
import time
1024
import sys
1125

26+
27+
28+
def isSQLite3(filename):
29+
"""
30+
Check if a file is a SQLite3 database.
31+
http://stackoverflow.com/questions/12932607/how-to-check-with-python-and-sqlite3-if-one-sqlite-database-file-exists
32+
"""
33+
from os.path import isfile, getsize
34+
35+
if not isfile(filename):
36+
return False
37+
if getsize(filename) < 100: # SQLite database file header is 100 bytes
38+
return False
39+
else:
40+
fd = open(filename, 'rb')
41+
Header = fd.read(100)
42+
fd.close()
43+
44+
if Header[0:16] == b'SQLite format 3\000':
45+
return True
46+
else:
47+
return False
48+
49+
50+
#
1251
# Get argument if there is one and use that as url
52+
#
1353
if len(sys.argv) == 2:
1454
url = sys.argv[1]
1555
else:
16-
url = "http://dbwebb.se/"
56+
sys.stderr.write("Add the url (for example http://bth.se) to check as a command line argument.\n")
57+
sys.stderr.write("{command} http://bth.se.\n\n".format(command=sys.argv[0]))
58+
sys.exit(1)
59+
60+
61+
62+
print(__doc__)
63+
_ = input("Press enter to continue.")
1764

18-
# connect to the database and set a cursor
19-
conn = sqlite3.connect("ping.sqlite3")
20-
cur = conn.cursor()
2165

22-
# init database incase it's lost
23-
cur.execute("""
24-
CREATE TABLE IF NOT EXISTS Pings
25-
(url TEXT, time TEXT PRIMARY KEY, repsonse INTEGER)""")
2666

67+
#
68+
# Connect to the database and set a cursor
69+
#
70+
dbFile = "ping.db"
71+
72+
db = sqlite3.connect(dbFile)
73+
dbc = db.cursor()
74+
75+
76+
#
77+
# Init a new database if needed
78+
#
79+
dbc.execute("CREATE TABLE IF NOT EXISTS Pings (url TEXT, time TEXT, repsonse INTEGER)")
80+
81+
82+
#
83+
# Check if the database is valid
84+
#
85+
if not isSQLite3(dbFile):
86+
sys.stderr.write("The file '{}' is not a valid SQLite database.\n".format(dbFile))
87+
sys.exit(1)
88+
89+
90+
91+
#
2792
# get current time for logging
93+
#
2894
rTime = time.ctime(time.time())
2995

3096
try:
3197
# url must start with "http://" or the request fails
98+
print("Connecting to: {}".format(url))
3299
r = requests.head(url)
33100
code = r.status_code
101+
34102
except requests.ConnectionError:
35103
code = 0
36104

37-
# create the values to be added as tuple
38-
values = (url, rTime, code)
105+
106+
107+
#
39108
# insert values into database
40-
cur.execute("""
41-
INSERT INTO Pings (url, time, repsonse)
42-
VALUES (?, ?, ?)
43-
""", values)
44-
45-
print("Rows in db:")
46-
# get all the rows in database
47-
cur.execute("SELECT url, time, repsonse FROM Pings")
48-
# loop through the result
49-
for row in cur:
109+
#
110+
print("Storing log entry in database")
111+
dbc.execute("INSERT INTO Pings (url, time, repsonse) VALUES (?, ?, ?)", (url, rTime, code))
112+
113+
print("Retrieving all rows in database:")
114+
dbc.execute("SELECT url, time, repsonse FROM Pings")
115+
116+
# Loop through a resultset from a SELECT query
117+
for row in dbc:
50118
print(row)
51119

52-
# commit the changes and close the connection
53-
conn.commit()
54-
conn.close()
120+
121+
122+
#
123+
# Commit the changes and close the connection
124+
#
125+
db.commit()
126+
db.close()

0 commit comments

Comments
 (0)