Rajhuggingface4253 commited on
Commit
0af9bf2
·
verified ·
1 Parent(s): ed002e3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, jsonify
2
+ import requests
3
+ import time
4
+ from datetime import datetime
5
+ import threading
6
+
7
+ app = Flask(__name__)
8
+
9
+ servers = [
10
+ "google.com",
11
+ "github.com",
12
+ "stackoverflow.com",
13
+ "docker.com",
14
+ "python.org"
15
+ ]
16
+
17
+ results = {}
18
+ last_run = None
19
+
20
+ def ping_server(url):
21
+ try:
22
+ start = time.time()
23
+ response = requests.get(url if '://' in url else f'https://{url}', timeout=10)
24
+ return {
25
+ 'status': 'success',
26
+ 'response_time': round((time.time() - start) * 1000, 1),
27
+ 'status_code': response.status_code
28
+ }
29
+ except:
30
+ return {'status': 'error'}
31
+
32
+ def ping_all():
33
+ global results, last_run
34
+ while True:
35
+ print(f"{datetime.now().strftime('%H:%M:%S')} - Pinging {len(servers)} servers...")
36
+ for server in servers:
37
+ results[server] = ping_server(server)
38
+ last_run = datetime.now()
39
+
40
+ success = sum(1 for r in results.values() if r['status'] == 'success')
41
+ print(f"✅ {success}/{len(servers)} servers OK")
42
+
43
+ time.sleep(300) # 5 minutes
44
+
45
+ @app.route('/')
46
+ def home():
47
+ success = sum(1 for r in results.values() if r['status'] == 'success') if results else 0
48
+ return f"""
49
+ <h1>Server Pinger</h1>
50
+ <p>Monitoring {len(servers)} servers</p>
51
+ <p>Last run: {last_run or 'Not yet'}</p>
52
+ <p>Status: {success}/{len(servers)} OK</p>
53
+ <p><a href="/results">View Results</a></p>
54
+ """
55
+
56
+ @app.route('/results')
57
+ def show_results():
58
+ return jsonify({
59
+ 'last_run': last_run.isoformat() if last_run else None,
60
+ 'results': results
61
+ })
62
+
63
+ # Start background pinging
64
+ threading.Thread(target=ping_all, daemon=True).start()
65
+
66
+ if __name__ == '__main__':
67
+ app.run(host='0.0.0.0', port=7860)