-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathweb_server.py
More file actions
92 lines (70 loc) · 2.7 KB
/
web_server.py
File metadata and controls
92 lines (70 loc) · 2.7 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import sys
import cgi
from http.server import HTTPServer, SimpleHTTPRequestHandler
from database.database import create_table, insert_record, fetch_records
HOST_NAME = "localhost"
PORT = 8080
def read_html_template(path):
"""function to read HTML file"""
try:
with open(path) as f:
file = f.read()
except Exception as e:
file = e
return file
def show_records(self):
"""function to show records in template"""
file = read_html_template(self.path)
# fetch records from database
table_data = fetch_records()
table_row = ""
for data in table_data:
table_row += "<tr>"
for item in data:
table_row += "<td>"
table_row += item
table_row += "</td>"
table_row += "</tr>"
# replace {{user_records}} in template by table_row
file = file.replace("{{user_records}}", table_row)
self.send_response(200, "OK")
self.end_headers()
self.wfile.write(bytes(file, "utf-8"))
class PythonServer(SimpleHTTPRequestHandler):
"""Python HTTP Server that handles GET and POST requests"""
def do_GET(self):
if self.path == '/':
self.path = './templates/form.html'
file = read_html_template(self.path)
self.send_response(200, "OK")
self.end_headers()
self.wfile.write(bytes(file, "utf-8"))
if self.path == '/show_records':
self.path = './templates/show_records.html'
# call show_records function to show users entered
show_records(self)
def do_POST(self):
if self.path == '/success':
ctype, pdict = cgi.parse_header(self.headers.get('content-type'))
pdict['boundary'] = bytes(pdict['boundary'], 'utf-8')
if ctype == 'multipart/form-data':
fields = cgi.parse_multipart(self.rfile, pdict)
full_name = fields.get("full_name")[0]
country = fields.get("country")[0]
# create table User if it runs first time else not
create_table()
# insert record into User table
insert_record(full_name, country)
html = f"<html><head></head><body><h1>Form data successfully recorded!!!</h1></body></html>"
self.send_response(200, "OK")
self.end_headers()
self.wfile.write(bytes(html, "utf-8"))
if __name__ == "__main__":
server = HTTPServer((HOST_NAME, PORT), PythonServer)
print(f"Server started http://{HOST_NAME}:{PORT}")
try:
server.serve_forever()
except KeyboardInterrupt:
server.server_close()
print("Server stopped successfully")
sys.exit(0)