-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
47 lines (38 loc) · 1.66 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from functools import partial
from http.server import BaseHTTPRequestHandler, HTTPServer
import ssl
from urllib.parse import urlparse,parse_qs
from threading import Thread
import config.main as config
class RequestHandler(BaseHTTPRequestHandler):
openingLock = None
password = None
def __init__(self, password, openingLock, *args, **kwargs):
self.openingLock = openingLock
self.password = password
super().__init__(*args, **kwargs)
def sendResponse(self, code, message = None):
self.send_response(code)
self.send_header('Content-type','text/html')
self.end_headers()
if message is None:
if code == 200:
self.wfile.write(b"OK! Lock is opening.")
if code == 403:
self.wfile.write(b"Acces denied")
else:
self.wfile.write(bytes(message))
def do_GET(self):
urlParsed = urlparse(self.path)
paramParsed = parse_qs(urlParsed.query)
#Testing parameters "pass" and "password"
if ('pass' in paramParsed and paramParsed['pass'][0] == self.password) or ('password' in paramParsed and paramParsed['password'][0] == self.password):
self.sendResponse(code=200)
self.openingLock()
else:
self.sendResponse(code=403)
class Server:
def start(self, password, openingLock, addr = '', port = 8000):
httpd = HTTPServer((addr, port), partial(RequestHandler, password, openingLock))
httpd.socket = ssl.wrap_socket (httpd.socket, keyfile=config.server['key_file'], certfile=config.server['cert_file'], server_side=True)
Thread(target=httpd.serve_forever).start()