181 lines
5.0 KiB
Python
181 lines
5.0 KiB
Python
import socket
|
|
from gs.classes.http_request import HttpRequest
|
|
|
|
|
|
class LittleApache():
|
|
|
|
available_wifis = []
|
|
keep_webserver_alive = True
|
|
|
|
|
|
def __init__(self, net):
|
|
self.net = net
|
|
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
|
|
self.s = socket.socket()
|
|
self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
self.s.bind(addr)
|
|
self.s.listen()
|
|
# print('Listening on', addr)
|
|
|
|
def start(self):
|
|
print("Webserver started. Connect to: " + self.net.ifconfig()[0])
|
|
print(self.net.ifconfig())
|
|
|
|
while self.keep_webserver_alive:
|
|
try:
|
|
conn, addr = self.s.accept()
|
|
#print('Got a connection from', addr)
|
|
|
|
# Receive and parse the request
|
|
request = conn.recv(1024)
|
|
# print("Request (RAW)", request)
|
|
http_request = HttpRequest(request)
|
|
self.http_request = http_request
|
|
request = str(request)
|
|
# print('Request content = %s' % request)
|
|
#try:
|
|
# request = request.split()[1]
|
|
# print('Request:', request)
|
|
#except IndexError:
|
|
# pass
|
|
response = self.response(http_request)
|
|
# Send the HTTP response and close the connection
|
|
conn.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
|
|
conn.send(response)
|
|
conn.close()
|
|
except OSError as e:
|
|
conn.close()
|
|
print('Connection closed')
|
|
return self
|
|
|
|
def response(self, request: HttpRequest):
|
|
#print("Webpage: ", request)
|
|
print("Request method: ", request.method, "Request path: ", request.path)
|
|
|
|
header = f"""
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>Growsystem</title>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
</head>
|
|
<body>
|
|
"""
|
|
|
|
footer = f"""
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
body = ""
|
|
|
|
if self.is_path_match(request, '/test1'):
|
|
body = f"""
|
|
<div>Test 1!!!</div>
|
|
"""
|
|
elif (self.is_path_match(request, '') or self.is_path_match(request, '/')):
|
|
options = []
|
|
for w in self.available_wifis:
|
|
options.append('<option value="{}">{}</option>'.format(w, w))
|
|
|
|
body = """
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Growsystem Setup</title>
|
|
<style>
|
|
body {{
|
|
font-family: Arial, sans-serif;
|
|
background-color: #f2f2f2;
|
|
margin: 0;
|
|
padding: 0;
|
|
display: flex;
|
|
justify-content: center;
|
|
align-items: center;
|
|
height: 100vh;
|
|
}}
|
|
|
|
form {{
|
|
background-color: #fff;
|
|
padding: 20px;
|
|
border-radius: 10px;
|
|
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
|
max-width: 400px;
|
|
width: 100%;
|
|
}}
|
|
|
|
h1 {{
|
|
text-align: center;
|
|
color: #333;
|
|
}}
|
|
|
|
input[type="submit"] {{
|
|
background-color: #4CAF50;
|
|
color: white;
|
|
padding: 10px 20px;
|
|
border: none;
|
|
border-radius: 5px;
|
|
cursor: pointer;
|
|
font-size: 16px;
|
|
}}
|
|
|
|
input[type="submit"]:hover {{
|
|
background-color: #45a049;
|
|
}}
|
|
|
|
input[type="text"],
|
|
input[type="password"],
|
|
input[type="number"],
|
|
select {{
|
|
width: 100%;
|
|
padding: 10px;
|
|
margin: 5px 0;
|
|
border: 1px solid #ccc;
|
|
border-radius: 5px;
|
|
box-sizing: border-box;
|
|
}}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<h1>Growsystem Setup</h1>
|
|
<form method="post" action="save">
|
|
<label for="ssid">SSID:</label><br>
|
|
<select name="ssid" id="ssid">
|
|
{}
|
|
</select><br>
|
|
<label for="password">Password:</label><br>
|
|
<input type="password" name="password" id="password"><br><br>
|
|
<label for="user_id">ID:</label><br>
|
|
<input type="number" name="user_id" id="user_id"><br>
|
|
<label for="pin">PIN:</label><br>
|
|
<input type="number" name="pin" id="pin" maxlength="4"><br>
|
|
<input type="submit" value="Submit">
|
|
</form>
|
|
</body>
|
|
</html>
|
|
"""
|
|
body = body.format(''.join(options))
|
|
elif self.is_path_match(request, '/save', 'POST'):
|
|
print("Save config path: ", request)
|
|
self.keep_webserver_alive = False
|
|
body = """
|
|
<h1>Setup Pico</h1>
|
|
<div>Setup abgeschlossen. Bitte ein paar Sekunden warten, dann neu starten.</div>
|
|
"""
|
|
else:
|
|
body = f"""
|
|
<div>Unknown page</div>
|
|
"""
|
|
html = ''
|
|
html_arr = [header, body, footer]
|
|
html = html.join(html_arr)
|
|
return str(html)
|
|
|
|
def is_path_match(self, request, path, method='GET'):
|
|
return path == request.path and method == request.method
|
|
|
|
|
|
|