This repository has been archived by the owner on Dec 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
wasseruhr.py
125 lines (103 loc) · 4.15 KB
/
wasseruhr.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
from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib import parse
import lib.ZaehlerstandClass
import os
import subprocess
import socketserver
import gc
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
global wasserzaehler
url_parse = parse.urlparse(self.path)
query_parse = parse.parse_qs(url_parse.query)
if 'reload' in url_parse.path:
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
result = 'Konfiguration wird neu geladen'
self.wfile.write(bytes(result, 'UTF-8'))
del wasserzaehler
gc.collect()
wasserzaehler = lib.ZaehlerstandClass.Zaehlerstand()
return
if "/image_tmp/" in url_parse.path:
self.send_response(200)
size = str(os.stat('.'+self.path).st_size)
self.send_header('Content-type', 'image/jpg')
self.send_header('Content-length', size)
self.end_headers()
with open('.'+self.path, 'rb') as file:
self.wfile.write(file.read()) # Read the file and send the contents
return
url = ''
if 'url' in query_parse:
url = query_parse['url'][0]
simple = True
if ('&full' in self.path) or ('?full' in self.path):
simple = False
single = False
if ('&single' in self.path) or ('?single' in self.path):
single = True
usePrevalue = False
if ('&usePreValue' in self.path) or ('?usePreValue' in self.path) or ('&usePrevalue' in self.path) or ('?usePrevalue' in self.path):
usePrevalue = True
value = ''
if 'value' in query_parse:
value = query_parse['value'][0]
if ('crash' in url_parse.path):
a = 1
b = 0
c = a / b
return
if ('version' in url_parse.path) or ('ROI' in url_parse.path):
result = "Version 6.1.1 (2020-04-23)"
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bytes(result, 'UTF-8'))
return
if ('crash' in url_parse.path):
result = "Crash in a second"
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bytes(result, 'UTF-8'))
print('Crash with division by zero!')
a = 1
b = 0
c = a/b
return
if ('roi' in url_parse.path) or ('ROI' in url_parse.path):
result = wasserzaehler.getROI(url)
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bytes(result, 'UTF-8'))
return
if 'setPreValue' in url_parse.path:
result = wasserzaehler.setPreValue(value)
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bytes(result, 'UTF-8'))
return
if 'wasserzaehler.json' in url_parse.path:
result = wasserzaehler.getZaehlerstandJSON(url, simple, usePrevalue, single)
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(bytes(result, 'UTF-8'))
return
if 'wasserzaehler' in url_parse.path:
result = wasserzaehler.getZaehlerstand(url, simple, usePrevalue, single)
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bytes(result, 'UTF-8'))
return
if __name__ == '__main__':
wasserzaehler = lib.ZaehlerstandClass.Zaehlerstand()
PORT = 3000
with socketserver.TCPServer(("", PORT), SimpleHTTPRequestHandler) as httpd:
print("Wasserzaehler is serving at port", PORT)
httpd.serve_forever()