malvika2003 commited on
Commit
2ee2c2c
·
verified ·
1 Parent(s): 5ab7f2b

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from http.server import BaseHTTPRequestHandler, HTTPServer
2
+ from urllib.parse import urlparse, parse_qs
3
+ from pathlib import Path
4
+
5
+ # Function to check credentials
6
+ def check_credentials(username, password):
7
+ # Replace with your authentication logic
8
+ return username == 'admin' and password == 'password'
9
+
10
+ class RequestHandler(BaseHTTPRequestHandler):
11
+ def do_GET(self):
12
+ parsed_path = urlparse(self.path)
13
+ path = parsed_path.path
14
+ if path == '/':
15
+ self._send_html_response('templates/index.html')
16
+ elif path == '/login':
17
+ self._send_html_response('templates/login.html')
18
+ else:
19
+ self.send_error(404, "File not found")
20
+
21
+ def do_POST(self):
22
+ content_length = int(self.headers['Content-Length'])
23
+ post_data = self.rfile.read(content_length).decode('utf-8')
24
+ params = parse_qs(post_data)
25
+ username = params['username'][0]
26
+ password = params['password'][0]
27
+
28
+ if check_credentials(username, password):
29
+ self.send_response(302)
30
+ self.send_header('Location', '/index.html')
31
+ self.end_headers()
32
+ else:
33
+ self._send_html_response('templates/login.html')
34
+
35
+ def _send_html_response(self, page):
36
+ path = Path(page)
37
+ if path.exists() and path.is_file():
38
+ self.send_response(200)
39
+ self.send_header('Content-type', 'text/html')
40
+ self.end_headers()
41
+ with open(page, 'rb') as file:
42
+ self.wfile.write(file.read())
43
+ else:
44
+ self.send_error(404, "File not found")
45
+
46
+ def run(server_class=HTTPServer, handler_class=RequestHandler, port=8000):
47
+ server_address = ('', port)
48
+ httpd = server_class(server_address, handler_class)
49
+ print(f'Starting httpd on port {port}...')
50
+ httpd.serve_forever()
51
+
52
+ if __name__ == '__main__':
53
+ run()