Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask, render_template, request, session, url_for, redirect, g
- from passlib.hash import sha512_crypt
- import pymysql as mc
- import hashlib
- import os
- # ,
- # dM
- # MMr
- # 4MMML .
- # MMMMM. xf
- # . "M6MMM .MM-
- # Mh.. +MM5MMM .MMMM
- # .MMM. .MMMMML. MMMMMh
- # )MMMh. MM5MMM MMMMMMM
- # 3MMMMx. 'MMM3MMf xnMMMMMM"
- # '*MMMMM MMMMMM. nMMMMMMP"
- # *MMMMMx "MMM5M\ .MMMMMMM=
- # *MMMMMh "MMMMM" JMMMMMMP
- # MMMMMM GMMMM. dMMMMMM .
- # MMMMMM "MMMM .MMMMM( .nnMP"
- # .. *MMMMx MMM" dMMMM" .nnMMMMM*
- # "MMn... 'MMMMr 'MM MMM" .nMMMMMMM*"
- # "4MMMMnn.. *MMM MM MMP" .dMMMMMMM""
- # ^MMMMMMMMx. *ML "M .M* .MMMMMM**"
- # *PMMMMMMhn. *x > M .MMMM**""
- # ""**MMMMhx/.h/ .=*"
- # .3P"%....
- # [4.20] nP" "*MMix
- app = Flask(__name__)
- app.secret_key = os.urandom(24)
- @app.route("/", methods=['POST', 'GET'])
- def login():
- if request.method == 'POST':
- session.pop('user', None)
- attempted_username = request.form['username']
- attempted_password = request.form['password']
- uname_algorithm = hashlib.md5()
- uname_algorithm.update(attempted_username.encode())
- uname = uname_algorithm.hexdigest()
- db = mc.connect("xxx.xxx.xx.xx", "dbuser", "your_password", "flaskapp")
- cursor = db.cursor()
- cmd = 'SELECT * FROM users WHERE username=%s'
- x = cursor.execute(cmd, (uname))
- if int(x) > 0:
- data = cursor.fetchone()[2]
- cursor.close()
- db.close()
- if sha512_crypt.verify(attempted_password, data):
- session['user'] = attempted_username
- return redirect(url_for('index'))
- else:
- return render_template('login.html', data0='Wrong username or password.')
- return render_template('login.html')
- @app.route("/register", methods=['POST', 'GET'])
- def register():
- if request.method == 'POST':
- attempted_username = request.form['username']
- attempted_password = request.form['password']
- if attempted_username > 5 and attempted_password > 7:
- uname_algorithm = hashlib.md5()
- uname_algorithm.update(attempted_username.encode())
- uname = uname_algorithm.hexdigest()
- pwd = sha512_crypt.encrypt(str(attempted_password))
- db = mc.connect("xxx.xxx.xx.xx", "dbuser", "your_password", "flaskapp")
- cursor = db.cursor()
- cmd = 'SELECT * FROM users WHERE username=%s'
- x = cursor.execute(cmd, (uname))
- if int(x) > 0:
- cursor.close()
- db.close()
- return render_template('register.html', data0='Username already exists!')
- else:
- cmd = "INSERT INTO users(username, password) VALUES( %s , %s )"
- cursor.execute(cmd, (uname, pwd))
- db.commit()
- cursor.close()
- db.close()
- return render_template('index.html', data0=uname, data1=pwd)
- return render_template('register.html')
- @app.route('/index')
- def index():
- if g.user:
- return render_template('index.html', data0=session['user'])
- return redirect(url_for('login'))
- @app.before_request
- def before_request():
- g.user = None
- if 'user' in session:
- g.user = session['user']
- if __name__ == "__main__":
- app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement