-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab5final
executable file
·108 lines (96 loc) · 3.17 KB
/
lab5final
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#! /usr/bin/python3
# Import modules for CGI handling
import cgi, cgitb
import os
import json
import MySQLdb
import passwords
cgitb.enable()
form = cgi.FieldStorage()
conn = MySQLdb.connect(host = passwords.SQL_HOST,
user = passwords.SQL_USER,
passwd = passwords.SQL_PASSWD,
db = "my_database")
def main_page():
print("Content-type: text/html")
print("Status: 200 OK")
print()
print("<head>")
print("<title>REST WEBSITE</title>")
print("</head>")
print("<body>")
print("<center>")
print("<br>")
print("<h1>Welcome to Twerk Prince Rental Car Company<br><br></h1> <h2>Hello~ %s %s</h2>" % ("Ian,", " Chaoneng"))
print("<h3>我与春风皆过客</h3>")
print("<h3>你携秋水揽星河</h3>")
print("<a href = '/cgi-bin/lab5final/listCars'> List all cars in database </a>")
print("<br>")
print("<a href = '/cgi-bin/lab5final/addCar'> Add a new car</a>")
print("</center>")
print("</body>")
def listCars():
print("Content-type: application/json")
print("Status: 200 OK")
print()
cursor = conn.cursor()
cursor.execute("SELECT * FROM cars;")
results = cursor.fetchall()
cursor.close()
records =[]
for line in results:
record = {"id":line[0], "brand":line[1], "model":line[2], "seats":line[3]}
records.append(record)
records_json = json.dumps(records, indent=2)
print(records_json)
def addCar():
print("Content-type: text/html")
print("Status: 200 OK")
print()
print("<center><form action = '/cgi-bin/lab5final/addCar' method = 'post'>")
print("Brand:<br>")
print("<input type = 'text' name = 'Brand'><br>")
print("Model:<br>")
print("<input type = 'text' name = 'Model'><br>")
print("Seats:<br>")
print("<input type = 'text' name = 'Seats'><br>")
print("<input type = 'submit' value = 'Submit'>")
brand = form.getvalue("Brand")
model = form.getvalue("Model")
seats = form.getvalue("Seats")
if brand and model and seats:
cursor1 = conn.cursor()
cursor1.execute("INSERT INTO cars (brand, model, seats) VALUES ('%s', '%s', %s);" % (brand,model,seats))
conn.commit()
single_car(cursor1.lastrowid)
cursor1.close()
def single_car(number):
print("Content-type: application/json")
print("Status: 200 OK")
print()
cursor = conn.cursor()
cursor.execute("SELECT * FROM cars;")
results = cursor.fetchall()
cursor.close()
for line in results:
if str(line[0]) == str(number):
single = [{"id":line[0], "brand":line[1], "model":line[2], "seats":line[3], "url":"http://ec2-3-222-78-23.compute-1.amazonaws.com/cgi-bin/lab5fianl/listCars/" + str(line[0])}]
single_json = json.dumps(single, indent=2)
print(single_json)
break
def redirectListCars():
print("Status: 302 Redirect")
print("Location: car")
print()
if 'PATH_INFO' in os.environ:
path = os.environ["PATH_INFO"]
else:
path = "/"
if path == "/":
main_page()
elif path == "/listCars" or path == "/listCars/":
listCars()
elif path == "/addCar" or path == "/addCar/":
addCar()
elif path.split("/")[2] != "":
single_car(path.split("/")[2])