Created
August 29, 2024 01:58
-
-
Save gdbassett/a50c40bf8429d13de5d55ce5e5855452 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
## start with salt_webapp.py in a separate line on the python configuration page int he DIGI web UI | |
### Import the server | |
from SimpleHTTPServer import SimpleHTTPRequestHandler | |
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler | |
from urlparse import urlparse, parse_qs | |
# https://python.readthedocs.io/en/v2.7.2/library/basehttpserver.html#BaseHTTPServer.BaseHTTPRequestHandler | |
Handler = BaseHTTPRequestHandler # class BaseHTTPServer.BaseHTTPRequestHandler(request, client_address, server) | |
class MyHandler(BaseHTTPRequestHandler): | |
def do_GET(self): | |
url_parts = urlparse(self.path) | |
query_params = parse_qs(url_parts.query) | |
if url_parts.path == "/logs/freshwateriq": | |
with open("./freshwater.log", 'rb') as filehandle: | |
self.send_response(200) | |
self.send_header('Content-type', 'text/csv') | |
self.end_headers() | |
self.wfile.write(filehandle.read()) | |
elif url_parts.path == "/logs/thinkworx": | |
with open("./gabe_thinkworx.csv", 'rb') as filehandle: | |
self.send_response(200) | |
self.send_header('Content-type', 'text/csv') | |
self.end_headers() | |
self.wfile.write(filehandle.read()) | |
else: | |
### this is all testing stuff V | |
param1 = query_params.get('param1', [''])[0] | |
param2 = query_params.get('param2', [''])[0] | |
print("param1: {param1}".format(**{"param1": param1})) | |
print("param2: {param2}".format(**{"param2": param2})) | |
self.send_response(200) | |
self.send_header('Content-type', 'text/html') | |
self.end_headers() | |
self.wfile.write("Debugging</br>") | |
self.wfile.write("{0}</br>".format(url_parts.path)) | |
self.wfile.write("param1: {param1}</br>".format(**{"param1": param1})) | |
self.wfile.write("param2: {param2}</br>".format(**{"param2": param2})) | |
### ^ | |
def main(): | |
# define the server | |
httpd = HTTPServer(('', 8000), MyHandler) | |
if __name__ == "__main__": | |
print('Starting salt server ...') | |
httpd.serve_forever() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment