-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
120 lines (97 loc) · 3.36 KB
/
main.py
File metadata and controls
120 lines (97 loc) · 3.36 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
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
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import webapp2
import cgi
from caesar import encrypt
# html boilerplate for the top of every page
page_header = """
<!DOCTYPE html>
<html>
<head>
<title>Caesar</title>
<style type="text/css">
.error {
color: red;
}
</style>
</head>
<body>
<h1>
<a href="/">Caesar</a>
</h1>
"""
# html boilerplate for the bottom of every page
page_footer = """
</body>
</html>
"""
edit_header = "<h2>Encrypt this text for me:</h2>"
cipher_form ="""
<form action="/cipher" method="post">
<textarea type="text" name="cipher-me" style="height:100px; width:350px;">
</textarea>
<br>
<label><h4>Rotate the characters by this amount:</h4><br>
<input type="number" min="1" value="1" name="rot"/>
</label>
<input type="submit"/>
</form>"""
class Index(webapp2.RequestHandler):
""" Handles requests coming in to '/' (the main page)"""
def get(self):
# if we have an error, make a <p> to display it
error = self.request.get("error")
error_element = "<p class='error'>" + error + "</p>" if error else ""
response = page_header + edit_header + cipher_form + error_element + page_footer
self.response.write(response)
class Cipher(webapp2.RequestHandler):
"""Handles request to encrypt user text"""
def post(self):
#look inside the request
cipher_me = self.request.get("cipher-me")
rotate = self.request.get("rot")
#print("text: " + cipher_me + "rotation: " + rotate)
# if no text was entered return the form with an error message
if not cipher_me:
error = "missing values"
error_escaped = cgi.escape(error, quote=True)
self.redirect("/?error=" + error_escaped)
elif rotate.isspace():
error = "missing values"
error_escaped = cgi.escape(error, quote=True)
self.redirect("/?error=" + error_escaped)
else:
# if text was entered, escape it and send it to the encryption function
cipher_me = cgi.escape(cipher_me, quote = True)
rotate = cgi.escape(rotate, quote = True)
ciphered = encrypt(cipher_me,int(rotate))
answer = """
<form action="/cipher" method="post">
<textarea type="text" name="cipher-me" style = "height:100px;
width:350px;">{0}</textarea>
<br>
<label><h4>Rotate the characters by this amount:</h4><br>
<input type="number" min="1" value="1" name="rot"/>
</label>
<input type="submit"/>
</form>""".format(ciphered)
response = page_header + edit_header + answer + page_footer
self.response.write(response)
app = webapp2.WSGIApplication([
('/', Index),
('/cipher', Cipher)
], debug=True)