-
Notifications
You must be signed in to change notification settings - Fork 0
/
tugas.py
44 lines (39 loc) · 1.54 KB
/
tugas.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
from http.server import HTTPServer, SimpleHTTPRequestHandler
import ssl
import base64
username = "itb1920"
password = "hidupmerdeka"
auth_keyword = username + ":" + password
auth_keyword_b64 = "Basic " + str(base64.b64encode(auth_keyword.encode("utf-8")),"utf-8")
class RequestHandler(http.server.SimpleHTTPRequestHandler):
def do_HEAD(self):
print("send header")
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_AUTHHEAD(self):
print("send header")
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):
#global auth_key
'''Present frontpage with user authentication.'''
if self.headers.get('authorization') == None:
self.do_AUTHHEAD()
self.wfile.write(b'no auth header received')
pass
elif self.headers.get('authorization') == auth_keyword_b64:
return SimpleHTTPRequestHandler.do_GET(self)
pass
else:
self.do_AUTHHEAD()
#print(self.headers.get('authorization'))
self.wfile.write(b'not authenticaticated')
pass
port = 4444
with HTTPServer(("",port), RequestHandler) as httpd:
print("serving at port ",port)
httpd.socket = ssl.wrap_socket(httpd.socket, keyfile = "key.pem", certfile = "certificate.pem", server_side=True)
httpd.serve_forever()