Mise à jour de 'gohttp.py'
This commit is contained in:
parent
ff7b26b9d3
commit
cbc84bcf1b
52
gohttp.py
52
gohttp.py
|
@ -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,10 +27,38 @@ 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):
|
||||||
|
|
||||||
|
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)
|
GPIO.setup(pinList[0], GPIO.OUT)
|
||||||
status0=int(GPIO.input(pinList[0]))
|
status0=int(GPIO.input(pinList[0]))
|
||||||
GPIO.setup(pinList[1], GPIO.OUT)
|
GPIO.setup(pinList[1], GPIO.OUT)
|
||||||
|
@ -39,18 +68,29 @@ class S(http.server.SimpleHTTPRequestHandler):
|
||||||
GPIO.setup(pinList[3], GPIO.OUT)
|
GPIO.setup(pinList[3], GPIO.OUT)
|
||||||
status3=int(GPIO.input(pinList[3]))
|
status3=int(GPIO.input(pinList[3]))
|
||||||
#logging.info("GET request,\nPath: %s\nHeaders:\n%s\n", str(self.path), str(self.headers))
|
#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()
|
self._set_response()
|
||||||
pass
|
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):
|
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')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user