Beginners – Python Flask Routes

I'm new to programming and backend. Right now I am trying to learn a bottle, so I have written a very simple website where the message is currently encrypted and decrypted with RSA and DES.

I think my app.py file is just bad and should do a lot better. I mean, there are a lot of repetitive code like this:

@app.route (& # 39; / rsa_encrypt & # 39 ;, Methods =['GET', 'POST'])
def RSA_enc ():
global get_form
if request.method == "GET":
get_form = true
return render_template (& # 39; rsa_encrypt.html & # 39 ;, gf = get_form)
otherwise:
get_form = False
rsa = RSA (request.form.get (? pt?), p = request.form.get (? p & # 39;),
q = request.form.get (# q #), e = request.form.get (e e & # 39;))
return render_template (& rs; rsa_encrypt.html & ggr ;, gf = get_form, rsa = rsa, gspn = gspn)

I tried to write some decorators for these routes, but they did not work well.

Can you take a look and tell me what to do better? It works, but I do not think as good as it should.

app.py

from flask import Flask, render_template, request, url_for, jsonify
of ciphers import RSA, DES
from primimport generate_semiprime_number as gspn

app = bottle (__ name__)
get_form = None

@app.route (& # 39; / & # 39;)
def index ():
return render_template (& # 39; index.html & # 39;

@app.route (& # 39; / rsa_encrypt & # 39 ;, Methods =['GET', 'POST'])
def RSA_enc ():
global get_form
if request.method == "GET":
get_form = true
return render_template (& # 39; rsa_encrypt.html & # 39 ;, gf = get_form)
otherwise:
get_form = False
rsa = RSA (request.form.get (? pt?), p = request.form.get (? p & # 39;),
q = request.form.get (# q #), e = request.form.get (e e & # 39;))
return render_template (& rs; rsa_encrypt.html & ggr ;, gf = get_form, rsa = rsa, gspn = gspn)

@app.route (& # 39; / rsa_decrypt & # 39 ;, method =['GET', 'POST'])
def RSA_dec ():
global get_form
if request.method == "GET":
get_form = true
return render_template (& # 39; rsa_decrypt.html & # 39; gf = get_form)
otherwise:
get_form = False
ct = [int(x) for x in request.form.get('ct')[1:-1].Teilt (# 39;, & # 39)]rsa = RSA (ciphertext = ct, p = request.form.get (#p #),
q = request.form.get (# q #), e = request.form.get (e e & # 39;))
return render_template (& rs; rsa_decrypt.html & gf; gf = get_form, rsa = rsa)

@app.route (& # 39; / des_encrypt & # 39 ;, method =['GET', 'POST'])
def DES_enc ():
global get_form
if request.method == "GET":
get_form = true
return render_template (& # 39; des_encrypt.html & # 39 ;, gf = get_form)
otherwise:
get_form = False
des = DES (request.form.get (& # 39; pt & # 39;), user_key = request.form.get (& # 39; key & # 39;))
return render_template (& # 39; des_encrypt.html & gf ;, gf = get_form, des = des)

@app.route (& # 39; / des_decrypt & # 39 ;, method =['GET', 'POST'])
def DES_dec ():
global get_form
if request.method == "GET":
get_form = true
return render_template (& # 39; des_decrypt.html & # 39 ;, gf = get_form)
otherwise:
get_form = False
des = DES (ciphertext = request.form.get (& # 39; ct & # 39;), user_key = request.form.get (& # 39; key & # 39;))
return render_template (& # 39; des_decrypt.html & gf; gf = get_form & des = des)

@app.route (& # 39; / gpsn & # 39 ;, method =['POST'])
def _gpsn ():
Returns jsonify ({# result; #): str (gspn (request.form['bits']))})


If __name__ == & # 39; __ main __ & # 39 ;:
app.run ()

Other files repository. It's also hosted so you can quickly take a look.