webserver.py 1.58 KiB
#!/usr/bin/env python3
# install
# pip3 install tornado
# see also
# https://www.tornadoweb.org/en/stable/guide/structure.html
# Tornado web server
import tornado.ioloop
import tornado.web
# General functionalities
import time
class MainHandler(tornado.web.RequestHandler):
def get(self):
temperature = 20.0
humidity = 42.0
# styles
css_text = "<style>\
table, th, td { border: 2px solid black; padding: 5px; border-spacing: 5px;\
background-color: #a0c0ff; }\
</style>"
# table including measured values
table = "<table>\
<tr><td>Temperature</td><td>" + "{:.1f} C".format(temperature) + "</td></tr>\
<tr><td>Humidity</td><td>" + "{:.1f} %".format(humidity) + "</td></tr>\
</table>"
# last update time
timeinfo = "<br/> Last update: " + time.strftime("%d.%m.%Y - %H:%M:%S h")
# page content to be displayed
html_text = "<!DOCTYPE html><html><head><title>Web Thermo-Hygrometer</title>" + css_text + \
"</head><body><h2>Web Thermo-Hygrometer</h2>" + table + timeinfo + "</body></html>"
# write page content
self.write(html_text)
# Connect MainHandler to App
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
])
# Main: let Tornado web server listen on port 8081
if __name__ == "__main__":
app = make_app()
app.listen(8081)
tornado.ioloop.IOLoop.current().start()