Advertisement
Guest User

Untitled

a guest
Mar 1st, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 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. x = cursor.execute(cmd, (uname))
  52. if int(x) > 0:
  53. data = cursor.fetchone()[2]
  54. cursor.close()
  55. db.close()
  56. if sha512_crypt.verify(attempted_password, data):
  57. session['user'] = attempted_username
  58. return redirect(url_for('index'))
  59.  
  60. else:
  61. return render_template('login.html', data0='Wrong username or password.')
  62.  
  63. return render_template('login.html')
  64.  
  65.  
  66. @app.route("/register", methods=['POST', 'GET'])
  67. def register():
  68. if request.method == 'POST':
  69. attempted_username = request.form['username']
  70. attempted_password = request.form['password']
  71. if attempted_username > 5 and attempted_password > 7:
  72. uname_algorithm = hashlib.md5()
  73. uname_algorithm.update(attempted_username.encode())
  74. uname = uname_algorithm.hexdigest()
  75. pwd = sha512_crypt.encrypt(str(attempted_password))
  76. db = mc.connect("xxx.xxx.xx.xx", "dbuser", "your_password", "flaskapp")
  77. cursor = db.cursor()
  78. cmd = 'SELECT * FROM users WHERE username=%s'
  79. x = cursor.execute(cmd, (uname))
  80. if int(x) > 0:
  81. cursor.close()
  82. db.close()
  83. return render_template('register.html', data0='Username already exists!')
  84.  
  85. else:
  86. cmd = "INSERT INTO users(username, password) VALUES( %s , %s )"
  87. cursor.execute(cmd, (uname, pwd))
  88. db.commit()
  89. cursor.close()
  90. db.close()
  91. return render_template('index.html', data0=uname, data1=pwd)
  92.  
  93. return render_template('register.html')
  94.  
  95.  
  96. @app.route('/index')
  97. def index():
  98. if g.user:
  99. return render_template('index.html', data0=session['user'])
  100.  
  101. return redirect(url_for('login'))
  102.  
  103.  
  104. @app.before_request
  105. def before_request():
  106. g.user = None
  107. if 'user' in session:
  108. g.user = session['user']
  109.  
  110. if __name__ == "__main__":
  111. app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement