Advertisement
Guest User

Untitled

a guest
Feb 28th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. from flask import Flask, render_template, request, session, url_for, redirect, g
  2. from passlib.hash import sha512_crypt
  3. import pymysql as mc
  4. import hashlib
  5. import os
  6.  
  7.  
  8.  
  9. # ,
  10. # dM
  11. # MMr
  12. # 4MMML .
  13. # MMMMM. xf
  14. # . "M6MMM .MM-
  15. # Mh.. +MM5MMM .MMMM
  16. # .MMM. .MMMMML. MMMMMh
  17. # )MMMh. MM5MMM MMMMMMM
  18. # 3MMMMx. 'MMM3MMf xnMMMMMM"
  19. # '*MMMMM MMMMMM. nMMMMMMP"
  20. # *MMMMMx "MMM5M\ .MMMMMMM=
  21. # *MMMMMh "MMMMM" JMMMMMMP
  22. # MMMMMM GMMMM. dMMMMMM .
  23. # MMMMMM "MMMM .MMMMM( .nnMP"
  24. # .. *MMMMx MMM" dMMMM" .nnMMMMM*
  25. # "MMn... 'MMMMr 'MM MMM" .nMMMMMMM*"
  26. # "4MMMMnn.. *MMM MM MMP" .dMMMMMMM""
  27. # ^MMMMMMMMx. *ML "M .M* .MMMMMM**"
  28. # *PMMMMMMhn. *x > M .MMMM**""
  29. # ""**MMMMhx/.h/ .=*"
  30. # .3P"%....
  31. # [4.20] nP" "*MMix
  32.  
  33.  
  34.  
  35.  
  36. app = Flask(__name__)
  37. app.secret_key = os.urandom(24)
  38.  
  39.  
  40. @app.route("/", methods=['POST', 'GET'])
  41. def login():
  42. if request.method == 'POST':
  43. session.pop('user', None)
  44. attempted_username = request.form['username']
  45. attempted_password = request.form['password']
  46. uname_algorithm = hashlib.md5()
  47. uname_algorithm.update(attempted_username.encode())
  48. uname = uname_algorithm.hexdigest()
  49. db = mc.connect("xxx.xxx.x.xx", "dbuser", "your_password", "flaskapp")
  50. cursor = db.cursor()
  51. cmd = 'SELECT * FROM users WHERE username=%s'
  52. cursor.execute(cmd, (uname))
  53. try:
  54. data = cursor.fetchone()[2]
  55. cursor.close()
  56. db.close()
  57. if sha512_crypt.verify(attempted_password, data):
  58. session['user'] = attempted_username
  59. return redirect(url_for('index'))
  60.  
  61. except:
  62. return render_template('login.html', data0='Wrong username or password.')
  63.  
  64. return render_template('login.html')
  65.  
  66.  
  67. @app.route("/register", methods=['POST', 'GET'])
  68. def register():
  69. if request.method == 'POST':
  70. attempted_username = request.form['username']
  71. attempted_password = request.form['password']
  72. if attempted_username > 5 and attempted_password > 7:
  73. uname_algorithm = hashlib.md5()
  74. uname_algorithm.update(attempted_username.encode())
  75. uname = uname_algorithm.hexdigest()
  76. pwd = sha512_crypt.encrypt(str(attempted_password))
  77. db = mc.connect("xxx.xxx.x.xx", "dbuser", "your_password", "flaskapp")
  78. cursor = db.cursor()
  79. cmd = 'SELECT * FROM users WHERE username=%s'
  80. x = cursor.execute(cmd, (uname))
  81. if int(x) > 0:
  82. cursor.close()
  83. db.close()
  84. return render_template('register.html', data0='Username already exists!')
  85.  
  86. else:
  87. cmd = "INSERT INTO users(username, password) VALUES( %s , %s )"
  88. cursor.execute(cmd, (uname, pwd))
  89. db.commit()
  90. cursor.close()
  91. db.close()
  92. return render_template('index.html', data0=uname, data1=pwd)
  93.  
  94. return render_template('register.html')
  95.  
  96.  
  97. @app.route('/index')
  98. def index():
  99. if g.user:
  100. return render_template('index.html', data0=session['user'])
  101.  
  102. return redirect(url_for('login'))
  103.  
  104.  
  105. @app.before_request
  106. def before_request():
  107. g.user = None
  108. if 'user' in session:
  109. g.user = session['user']
  110.  
  111. if __name__ == "__main__":
  112. app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement