-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
100 lines (74 loc) · 2 KB
/
main.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
from flask import Flask, request
app = Flask(__name__)
app.config['DEBUG'] = True
form = '''<!DOCTYPE html>
<html>
<head>
<style>
form {{
background-color: #eee;
padding: 20px;
margin: 0 auto;
width: 540px;
font: 16px sans-serif;
border-radius: 10px;
}}
textarea {{
margin: 10px 0;
width: 540px;
height: 120px;
}}
</style>
</head>
<body>
<form action="/rotate" method="post">
<label for="rot">Rotate by:</label><br>
<input type="text" id="rot" name="rot" value="{0}" /><br>
<textarea id="text" name="text">{1}</textarea>
<input type="submit" value="sumbit" />
</form>
</body>
</html>
'''
def alphabet_position(letter):
up_alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
low_alpha = 'abcdefghijklmnopqrstuvwxyz'
if letter.isupper():
return up_alpha.index(letter)
elif letter.islower():
return low_alpha.index(letter)
def rotate_character(char, rot):
new_char =''
up_alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
low_alpha = 'abcdefghijklmnopqrstuvwxyz'
if char.isalpha():
if char.isupper():
index = alphabet_position(char)
new_char = up_alpha[(index+rot) % 26]
return new_char
if char.islower():
index = alphabet_position(char)
new_char = low_alpha[(index+rot) % 26]
return new_char
else:
new_char = char
return char
def encrypt(text, rot):
new_text = ''
for c in text:
new_text += rotate_character(c,rot)
return new_text
@app.route("/")
def index():
return form.format(0, "")
@app.route("/hello")
def hello():
first_name = request.args.get("first_name")
return '<h1>Hello, ' + first_name + '</h1>'
@app.route("/rotate", methods=["POST"])
def rotate():
text = request.form["text"]
rot = int(request.form["rot"])
final = encrypt(text, rot)
return form.format(rot, final)
app.run(port="8080")