-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
62 lines (56 loc) · 1.7 KB
/
server.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
import http.server
import socketserver
from urllib.parse import urlparse
from urllib.parse import parse_qs
import os
import requests
green = 0
red = 0
class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'image/png')
self.end_headers()
# print (self.path)
path = self.path
ss = path.split('-')
# print (ss)
if(len(ss) == 2):
if(ss[0] == '/ping'):
#do ping
# print ('ping ' + ss[1])
response = os.system("ping -W 0.5 -c 1 " + ss[1])
if(response == 0):
self.wfile.write(green)
else:
self.wfile.write(red)
if(ss[0] == '/load'):
#do load
# print ('load' + ss[1])
try:
response = requests.get(ss[1], timeout=1)
except:
self.wfile.write(red)
return
if(response.status_code == 200):
self.wfile.write(green)
else:
self.wfile.write(red)
return
r = open('/path/to/red.png', 'rb')
g = open('/path/to/green.png', 'rb')
red = r.read()
green = g.read()
# Create an object of the above class
handler_object = MyHttpRequestHandler
PORT = 8000
try:
my_server = socketserver.TCPServer(("", PORT), handler_object)
print('Server started on port ' + str(PORT))
# Star the server
my_server.serve_forever()
except KeyboardInterrupt:
print ('^C received, shutting down the web server')
my_server.socket.close()
r.close()
g.close()