-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathrun_server.py
More file actions
156 lines (102 loc) · 4.21 KB
/
run_server.py
File metadata and controls
156 lines (102 loc) · 4.21 KB
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
import os
import time
import datetime
from PIL import Image
from http.server import HTTPServer, BaseHTTPRequestHandler
import secrets
import socket
from weather_landscape import WeatherLandscape
from configs import *
SERV_IPADDR = "0.0.0.0"
SERV_PORT = 3355
FAVICON = "favicon.ico"
FILETOOOLD_SEC = 60*10
WEATHERS = [ WeatherLandscape(WLConfig_BW()) ,
WeatherLandscape(WLConfig_BWI()) ,
WeatherLandscape(WLConfig_EINK()) ,
WeatherLandscape(WLConfig_RGB_Black()) ,
WeatherLandscape(WLConfig_RGB_White()) ,
]
class WeatherLandscapeServer(BaseHTTPRequestHandler):
def do_GET_sendfile(self,filepath:str,mimo:str):
try:
f = open(filepath, "rb")
databytes = f.read()
f.close()
except Exception as e:
databytes = None
print("File read error '%s' :%s" % (filepath,str(e)))
if (databytes!=None):
self.send_response(200)
self.send_header("Content-type", mimo)
else:
self.send_response(404)
self.end_headers()
if (databytes!=None):
self.wfile.write(databytes)
def do_GET(self):
if self.path == '/':
self.path = '/index.html'
print("GET:",self.path)
if (self.path.startswith('/'+FAVICON)):
self.do_GET_sendfile(FAVICON,"image/ico")
return
if (self.path.startswith('/index.html')):
self.send_response(200)
self.end_headers()
self.wfile.write(bytes(self.IndexHtml(), 'utf-8'))
return
for w in WEATHERS:
if self.path == '/'+w.cfg.OUT_FILENAME:
file_name = self.CreateWeatherImage(w)
mime = w.cfg.GetMIME()
assert mime!=None, "Unsuported image file extension"
self.do_GET_sendfile(file_name ,mime)
return
print("Path not accessible:",self.path)
self.send_response(403)
self.end_headers()
def IsFileTooOld(self, filename):
return (not os.path.isfile(filename)) or ( (time.time() - os.stat(filename).st_mtime) > FILETOOOLD_SEC )
def CreateWeatherImage(self,weather):
file_name = weather.cfg.ImageFilePath()
if not self.IsFileTooOld(file_name):
return file_name
return weather.SaveImage()
def IndexHtml(self):
body = '<h1>Weather as Landscape</h1>'
for w in WEATHERS:
id = 'id_'+w.cfg.OUT_FILENAME
body+='<h2>'+w.cfg.TITLE+'</h2>'
#body+='<p>Place: '+("%.4f" % w.cfg.OWM_LAT) +' , '+("%.4f" % w.cfg.OWM_LON)+'</p>'
body+='<p><img src="'+w.cfg.OUT_FILENAME+'" alt="'+w.cfg.TITLE+'" "></p>'
body+='<h4>URL: <span id="'+id+'"></span></h4>'
body+='<script> document.getElementById("'+id+'").innerHTML = window.location+"'+w.cfg.OUT_FILENAME+'" ;</script>'
body+='<p> </p>'
return """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Weather as Landscape</title>
</head>
<body> """ + body + """
</body>
</html>"""
#todo: implement support for multiple network interfaces
def get_my_ips():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(('8.8.8.8', 80))
yield s.getsockname()[0]
finally:
s.close()
httpd = HTTPServer((SERV_IPADDR,SERV_PORT),WeatherLandscapeServer)
for ip in get_my_ips():
print(r"Serving at http://%s:%i/" % (ip,SERV_PORT))
httpd.serve_forever()
# IPV6
#class HTTPServerV6(HTTPServer):
# address_family = socket.AF_INET6
#httpd = HTTPServerV6(('::',SERV_PORT),WeatherLandscapeServer)
#httpd.serve_forever()