-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
166 lines (136 loc) · 5.5 KB
/
app.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
from http.server import HTTPServer, BaseHTTPRequestHandler
from collections import OrderedDict
import webbrowser
import cgi
import cgitb
import js2py
cgitb.enable()
# testing javascript in python (unfinished)
js1 = 'console.log("hi whatsup man")'
res1 = js2py.eval_js(js1)
res1
# import all webpages
Game = open("src/Game.html").read()
GameJS = open("src/Game.js").read()
Username = open("src/Username.html").read()
Password = open("src/Password.html").read()
# List of usernames
userlist = [
'user1',
'user2',
'user3',
'user4',
'user5',
'user6',
'user7',
'user8',
'user9',
'user10',
'user11',
'user12',
'user13',
'user14',
'user15']
userinput = "|".join(userlist)
# List of passwords
passlist = ['pass1',
'pass2',
'pass3',
'pass4',
'pass5',
'pass6',
'pass7',
'pass8',
'pass9',
'pass10',
'pass11',
'pass12',
'pass13',
'user14',
'pass15']
# Ordered dictionary of table values
tableVal = OrderedDict()
for i in range(0, 15):
tableVal[userlist[i]] = ""
tableVal = list(tableVal.items())
class requestHandler(BaseHTTPRequestHandler):
def do_GET(self): # put all GET requests in this category
if self.path.endswith('/Username'): # Display the page for URL: "http://127.0.0.1:8080/Username"
self.send_response(200)
self.send_header('content-type', 'text/html')
self.end_headers()
output = Username.replace("anvnamn", userinput) # insert list of valid usernames into placeholder.
self.wfile.write(output.encode())
if self.path.endswith('/Password'): # Display the page for URL: "http://127.0.0.1:8080/Password"
self.send_response(200)
self.send_header('content-type', 'text/html')
self.end_headers()
output = Password.replace("losen", reqpass) # insert list of valid passwords into placeholder
self.wfile.write(output.encode())
if self.path.endswith('/Game'): # Display the page for URL: "http://127.0.0.1:8080/Game"
self.send_response(200)
self.send_header('content-type', 'text/html')
self.end_headers()
# Load specified username + Javascript into Game.html
output = Game\
.replace("<script></script>", "<script>" + GameJS + "</script>") \
# insert initial table values
for i in range(1, 16):
output = output.replace(
"score"+str(i)+" ",
str(tableVal[i-1][1])
) # insert list of scores into placeholders in the table
output = output.replace(
"username"+str(i)+" ",
tableVal[i-1][0]
# <td id="15score">score15 </td>
) # insert list of usernames into placeholders in the table
# sessionStorage.getItem("score1")
output = output.replace("specUser", uservar)
self.wfile.write(output.encode())
def do_POST(self): # put all POST requests in this category
if self.path=="/Password": # Determine whether user is qualified to get redirected to URL: "http://127.0.0.1:8080/Password"
global uservar
global reqpass
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD': 'POST',
'CONTENT_TYPE':self.headers['Content-Type']}
)
# Store user's input username in variable
uservar = form['user']
uservar = str(uservar)
uservar = uservar.replace("FieldStorage('user', None, '", "")
uservar = uservar.replace("')", "")
print(uservar)
# Identify which password that matches the given username
userindex = userlist.index(uservar)
reqpass = passlist[userindex]
print(reqpass)
self.send_response(301)
self.send_header('Location', '/Password')
self.end_headers()
if self.path=="/Game": # Determine whether user is qualified to get redirected to URL: "http://127.0.0.1:8080/Game"
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD': 'POST',
'CONTENT_TYPE':self.headers['Content-Type']}
)
passvar = form['passw']
passvar = str(passvar)
passvar = passvar.replace("FieldStorage('passw', None, '", "")
passvar = passvar.replace("')", "")
self.send_response(301)
self.send_header('Location', '/Game')
self.end_headers()
def main():
PORT = 8080 # specify which port you want to run the app on
server_address = ('localhost', PORT)
server = HTTPServer(server_address, requestHandler)
print('Server running on port {}'.format(PORT)) # Display in console if the server is running
webbrowser.open('http://127.0.0.1:8080/Username') # Open the app's URL in browser
server.serve_forever() # run the app
if __name__ == "__main__":
main()