Mise à jour de 'gohttp.py'

This commit is contained in:
Xavier 2022-09-09 19:45:33 +02:00
parent ff7b26b9d3
commit cbc84bcf1b

View File

@ -14,6 +14,7 @@ import subprocess
import cgi import cgi
import time import time
import json import json
import base64
fileObject = open("config.json", "r") fileObject = open("config.json", "r")
jsonContent = fileObject.read() jsonContent = fileObject.read()
@ -26,31 +27,70 @@ GPIO.setmode(GPIO.BCM)
pinList = [2, 3, 4, 17] pinList = [2, 3, 4, 17]
SleepTimeL=10 SleepTimeL=10
#https://forums.raspberrypi.com/viewtopic.php?t=24389
#
#YW5vdGhlcjptZQ==
#another:me
#
#admin:123456
#YWRtaW46MTIzNDU2
class S(http.server.SimpleHTTPRequestHandler): 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): def do_GET(self):
GPIO.setup(pinList[0], GPIO.OUT) if self.headers['Authorization'] == None:
status0=int(GPIO.input(pinList[0])) self.do_AUTHHEAD()
GPIO.setup(pinList[1], GPIO.OUT) #self.wfile.write(bytes('no auth header received', 'UTF-8'))
status1=int(GPIO.input(pinList[1])) pass
GPIO.setup(pinList[2], GPIO.OUT) elif self.headers['Authorization'] == 'Basic YWRtaW46MTIzNDU2':
status2=int(GPIO.input(pinList[2])) self.do_HEAD()
GPIO.setup(pinList[3], GPIO.OUT) #self.wfile.write(bytes(self.headers['Authorization'], 'UTF-8'))
status3=int(GPIO.input(pinList[3])) #self.wfile.write(bytes(' authenticated!', 'UTF-8'))
#logging.info("GET request,\nPath: %s\nHeaders:\n%s\n", str(self.path), str(self.headers)) 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
self._set_response()
pass
def do_POST(self): def do_POST(self):
content_length = int(self.headers['Content-Length']) # <--- Gets the size of data content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
post_data = self.rfile.read(content_length) # <--- Gets the data itself 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", #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')) #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): def _set_response(self):
@ -70,7 +110,7 @@ class S(http.server.SimpleHTTPRequestHandler):
status3=int(GPIO.input(pinList[3])) status3=int(GPIO.input(pinList[3]))
objParsedPath = str(format(self.path).encode('utf-8')) objParsedPath = str(format(self.path).encode('utf-8'))
print ('objParsedPath="'+objParsedPath+'"') #print ('objParsedPath="'+objParsedPath+'"')
if objParsedPath == "b'/R0'": if objParsedPath == "b'/R0'":
print ('R0 run') print ('R0 run')