Advertisement
Guest User

Untitled

a guest
Mar 1st, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 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. # ,
  11. # dM
  12. # MMr
  13. # 4MMML .
  14. # MMMMM. xf
  15. # . "M6MMM .MM-
  16. # Mh.. +MM5MMM .MMMM
  17. # .MMM. .MMMMML. MMMMMh
  18. # )MMMh. MM5MMM MMMMMMM
  19. # 3MMMMx. 'MMM3MMf xnMMMMMM"
  20. # '*MMMMM MMMMMM. nMMMMMMP"
  21. # *MMMMMx "MMM5M\ .MMMMMMM=
  22. # *MMMMMh "MMMMM" JMMMMMMP
  23. # MMMMMM GMMMM. dMMMMMM .
  24. # MMMMMM "MMMM .MMMMM( .nnMP"
  25. # .. *MMMMx MMM" dMMMM" .nnMMMMM*
  26. # "MMn... 'MMMMr 'MM MMM" .nMMMMMMM*"
  27. # "4MMMMnn.. *MMM MM MMP" .dMMMMMMM""
  28. # ^MMMMMMMMx. *ML "M .M* .MMMMMM**"
  29. # *PMMMMMMhn. *x > M .MMMM**""
  30. # ""**MMMMhx/.h/ .=*"
  31. # .3P"%....
  32. # [4.20] nP" "*MMix
  33.  
  34.  
  35. app = Flask(__name__)
  36. app.secret_key = os.urandom(24)
  37.  
  38.  
  39. @app.route("/", methods=['POST', 'GET'])
  40. def login():
  41. if request.method == 'POST':
  42. session.pop('user', None)
  43. attempted_username = request.form['username']
  44. attempted_password = request.form['password']
  45. uname_algorithm = hashlib.md5()
  46. uname_algorithm.update(attempted_username.encode())
  47. uname = uname_algorithm.hexdigest()
  48. db = mc.connect("xxx.xxx.xx.xx", "dbuser", "your_password", "flaskapp")
  49. cursor = db.cursor()
  50. cmd = 'SELECT * FROM users WHERE username=%s'
  51. cursor.execute(cmd, (uname))
  52. if int(x) > 0:
  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.xx.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