diff --git a/gohttp.py b/gohttp.py index ec3da76..2c4f9f0 100755 --- a/gohttp.py +++ b/gohttp.py @@ -14,6 +14,7 @@ import subprocess import cgi import time import json +import base64 fileObject = open("config.json", "r") jsonContent = fileObject.read() @@ -26,31 +27,70 @@ GPIO.setmode(GPIO.BCM) pinList = [2, 3, 4, 17] SleepTimeL=10 -class S(http.server.SimpleHTTPRequestHandler): - - def do_GET(self): - - GPIO.setup(pinList[0], GPIO.OUT) - status0=int(GPIO.input(pinList[0])) - GPIO.setup(pinList[1], GPIO.OUT) - status1=int(GPIO.input(pinList[1])) - GPIO.setup(pinList[2], GPIO.OUT) - status2=int(GPIO.input(pinList[2])) - GPIO.setup(pinList[3], GPIO.OUT) - status3=int(GPIO.input(pinList[3])) - #logging.info("GET request,\nPath: %s\nHeaders:\n%s\n", str(self.path), str(self.headers)) - self._set_response() - pass +#https://forums.raspberrypi.com/viewtopic.php?t=24389 +# +#YW5vdGhlcjptZQ== +#another:me +# +#admin:123456 +#YWRtaW46MTIzNDU2 +class S(http.server.SimpleHTTPRequestHandler): + + + def do_HEAD(self): + self.send_response(200, "OK") + self.send_header('Content-type', 'text/html') + self.end_headers() + + def do_AUTHHEAD(self): + self.send_response(401) + self.send_header('WWW-Authenticate', 'Basic realm=\"Test\"') + self.send_header('Content-type', 'text/html') + self.end_headers() + + def do_GET(self): + + if self.headers['Authorization'] == None: + self.do_AUTHHEAD() + #self.wfile.write(bytes('no auth header received', 'UTF-8')) + pass + elif self.headers['Authorization'] == 'Basic YWRtaW46MTIzNDU2': + self.do_HEAD() + #self.wfile.write(bytes(self.headers['Authorization'], 'UTF-8')) + #self.wfile.write(bytes(' authenticated!', 'UTF-8')) + GPIO.setup(pinList[0], GPIO.OUT) + status0=int(GPIO.input(pinList[0])) + GPIO.setup(pinList[1], GPIO.OUT) + status1=int(GPIO.input(pinList[1])) + GPIO.setup(pinList[2], GPIO.OUT) + status2=int(GPIO.input(pinList[2])) + GPIO.setup(pinList[3], GPIO.OUT) + status3=int(GPIO.input(pinList[3])) + #logging.info("GET request,\nPath: %s\nHeaders:\n%s\n", str(self.path), str(self.headers)) + #self.send_response(200, "OK") + #self.end_headers() + self._set_response() + pass + else: + self.do_AUTHHEAD() + self.wfile.write(bytes(self.headers['Authorization'], 'UTF-8')) + self.wfile.write(bytes(' not authenticated', 'UTF-8')) + logging.info('Someone wanted to get access without authorization') + file = read_html_template("login.html") + self.wfile.write(bytes(file.encode("utf-8"))) + pass + + def do_POST(self): content_length = int(self.headers['Content-Length']) # <--- Gets the size of data post_data = self.rfile.read(content_length) # <--- Gets the data itself - logging.info("POST request,\nPath: %s\nHeaders:\n%s\n\nBody:\n%s\n", - str(self.path), str(self.headers), post_data.decode('utf-8')) + #logging.info("POST request,\nPath: %s\nHeaders:\n%s\n\nBody:\n%s\n", + #str(self.path), str(self.headers), post_data.decode('utf-8')) + #self._set_response() + - self._set_response() - self.wfile.write("POST request for {}".format(self.path).encode('utf-8')) def _set_response(self): @@ -70,7 +110,7 @@ class S(http.server.SimpleHTTPRequestHandler): status3=int(GPIO.input(pinList[3])) objParsedPath = str(format(self.path).encode('utf-8')) - print ('objParsedPath="'+objParsedPath+'"') + #print ('objParsedPath="'+objParsedPath+'"') if objParsedPath == "b'/R0'": print ('R0 run')