This repository has been archived by the owner on Jul 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
60 lines (53 loc) · 1.86 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
from flask import Flask, render_template, request, jsonify
import morsecode_core
app = Flask(__name__)
# Root page.
@app.route('/')
def index():
return render_template('index.html')
# Handle the post request to convert the provided message to cipher.
@app.route('/encrypt/', methods=['POST'])
def encrypt():
# Return the error if the message is not provided in the request.
if 'message' not in request.form:
return (jsonify({
'status' : 'error',
'message': 'Message is not provided.'
}), 400)
try:
# Covert the message and return the cipher with success status code.
message = request.form['message']
return (jsonify({
'status' : 'success',
'cipher': morsecode_core.encrypt(message.strip())
}), 200)
except:
# In case of exception, throw the internal error status code.
return (jsonify({
'status' : 'error',
'message': 'Unknown exception occured.'
}), 500)
# Handle the post request to convert the provided cipher to message.
@app.route('/decrypt/', methods=['POST'])
def decrypt():
# Return the error if the cipher is not provided in the request.
if 'cipher' not in request.form:
return (jsonify({
'status' : 'error',
'message': 'Cipher is not provided.'
}), 400)
try:
# Covert the cipher and return the text with success status code.
cipher = request.form['cipher']
return (jsonify({
'status' : 'success',
'message': morsecode_core.decrypt(cipher.strip())
}), 200)
except:
# In case of exception, throw the internal error status code.
return (jsonify({
'status' : 'error',
'message': 'Unknown exception occured.'
}), 500)
if __name__ == '__main__':
app.run()