-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
executable file
·190 lines (180 loc) · 5.91 KB
/
app.py
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from flask import Flask, render_template
from flask import request
import psycopg2
import re
import base64
import os, time, http.client
import requests
import urllib.request
import threading
import time
app = Flask(__name__)
def run_check():
threading.Timer(60.0, run_check).start()
SITES = []
SITE_ID = {}
try:
conn = psycopg2.connect(host="localhost",database="websitechecker", user="postgres", password="")
"""
DATABASE_URL = os.environ['DATABASE_URL']
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
"""
cur = conn.cursor()
query = "SELECT * from websites"
cur.execute(query)
results = cur.fetchall()
#"""
for res in results:
#print(res)
SITES.append(res[0])
SITE_ID[res[0]] = res[1]
#"""
conn.close()
except Exception as e:
print(e)
for site in SITES:
sheraa = site
if sheraa == "":
break
global response_status
response_time = 0
try:
# conn = http.client.HTTPSConnection(site+'/?', timeout=10)
# conn.request("HEAD", "/?")
# response = conn.getresponse()
response = requests.get(sheraa);
if response.status_code != 200:
response_status = 'DOWN'
else:
response_time = requests.get(sheraa).elapsed.total_seconds()
response_status = 'OK'
print (site+" " + str(response.status_code)+" " + str(response_status)+" " +str(response_time))
try:
conn = psycopg2.connect(host="localhost",database="websitechecker", user="postgres", password="")
"""
DATABASE_URL = os.environ['DATABASE_URL']
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
"""
cur = conn.cursor()
query = "INSERT INTO websiteinfo (site_name, site_id, response_code, response_type, response_time) VALUES (%s, %s,%s,%s,%s)"
values = (sheraa, SITE_ID[sheraa], str(response.status_code), str(response_status), str(response_time))
cur.execute(query, values)
conn.commit()
count = cur.rowcount
print(count)
"""
filelist = []
for res in results:
filelist.append(res[1])
print(res)
count+=1
"""
conn.close()
except Exception as e:
print(e)
conn.close()
except Exception as e:
print (site+" "+str(e))
run_check()
@app.route('/')
def runpy():
global datacount
filelist = []
print("reloading....")
return render_template('index.html')
@app.route('/',methods=['post'])
def form_post():
global sitename
sitename = request.form['addr']
print(sitename)
if(sitename != ""):
try:
conn = psycopg2.connect(host="localhost",database="websitechecker", user="postgres", password="")
"""
DATABASE_URL = os.environ['DATABASE_URL']
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
"""
cur = conn.cursor()
query = "INSERT INTO websites (site_name, monitoring_from) VALUES (%s, current_timestamp)"
values = (sitename,)
cur.execute(query, values)
conn.commit()
count = cur.rowcount
print(count)
"""
filelist = []
for res in results:
filelist.append(res[1])
print(res)
count+=1
"""
conn.close()
except Exception as e:
print(e)
try:
conn = psycopg2.connect(host="localhost",database="websitechecker", user="postgres", password="")
"""
DATABASE_URL = os.environ['DATABASE_URL']
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
"""
cur = conn.cursor()
query = "SELECT * from websites"
cur.execute(query)
results = cur.fetchall()
#"""
for res in results:
print(res)
#"""
conn.close()
except Exception as e:
print(e)
return render_template('dashboard.html', results=results)
@app.route('/dashboard')
def dashboard():
print("dashboard....")
try:
conn = psycopg2.connect(host="localhost",database="websitechecker", user="postgres", password="")
"""
DATABASE_URL = os.environ['DATABASE_URL']
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
"""
cur = conn.cursor()
query = "SELECT * from websites"
cur.execute(query)
results = cur.fetchall()
#"""
for res in results:
print(res)
#"""
conn.close()
except Exception as e:
print(e)
return render_template('dashboard.html', results=results)
@app.route('/dashboard/<int:website_id>')
def websiteinfo(website_id):
print(website_id)
global website_name
print("dashboard....")
try:
conn = psycopg2.connect(host="localhost",database="websitechecker", user="postgres", password="")
"""
DATABASE_URL = os.environ['DATABASE_URL']
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
"""
cur = conn.cursor()
query = "SELECT * from websiteinfo WHERE site_id = %s"
values = (website_id,)
cur.execute(query, values)
results = cur.fetchall()
#"""
for res in results:
website_name = res[0]
#"""
conn.close()
except Exception as e:
print(e)
return render_template('dashboard-websiteinfo.html', results=results, website_name=website_name)
if __name__ == '__main__':
app.debug = True
app.run()
app.run(debug=True)