/
mainrot.py
69 lines (55 loc) · 2.78 KB
/
mainrot.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
import webapp2
import jinja2
import os
templateDir = os.path.join(os.path.dirname(__file__), 'templates')
jinjaEnv = jinja2.Environment(loader = jinja2.FileSystemLoader(templateDir),
autoescape = True)
class Handler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.write(*a, **kw)
def renderStr(self, template, **params): #template here refers to a file name, set as a parameter in this function
t = jinjaEnv.get_template(template) #this stmt means jinja2 will call the file entered using its get_template function, and save the object in a variable
return t.render(params) #it will then call its render function on the object with the parameters set to it, and return a string
def render(self, template, **kw):
self.write(self.renderStr(template, **kw))
def rot13(a):
alp = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
count = ""
x = 0
for i in a:
if i in alp and alp.index(i) < 13 and i.islower() and x < len(a):
count = count + alp[alp.index(i) + 13]
x += 1
elif i in alp and 13 <= alp.index(i) <= 25 and i.islower() and x < len(a):
count = count + alp[alp.index(i) - 13]
x += 1
elif i in alp and 25 <= alp.index(i) <= 38 and x < len(a):
count = count + alp[alp.index(i) + 13]
x += 1
elif i in alp and alp.index(i) > 38 and x < len(a):
count = count + alp[alp.index(i) - 13]
x += 1
elif i not in alp:
count = count + i
x +=1
return count
class MainPage(Handler):
def get(self):
self.render("crypto.html")
def post(self):
wod = self.request.get("text") #text refers to the text entered to the name attr in the html form. request.get always returns a string
ent = rot13(wod)
self.render("crypto.html", some=ent) #if you want any data returned in the same textarea box,
#instantiate it in the html textarea box and reference it here "text=wod" i.e. the word at the left hand side of the assignment statement is the variable to be printed out in the html file and the right hand side is the value to be printed out
#this is the handler for the form: when you send a GET request, the url is '/'
# class MainPage(webapp2.RequestHandler):
# def get(self):
# #self.response.headers['Content-Type'] = 'text/html'
# self.response.write(form)
#
# def post(self):
# wod = self.request.get("text") #text refers to the text entered to the name attr in the html form above
# self.response.write(wod)
app = webapp2.WSGIApplication([
('/', MainPage)
], debug=True)