Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. import random
  2. from flask import Flask, render_template, redirect, url_for, request
  3. import pymysql
  4. from flask_bcrypt import Bcrypt
  5. import hashlib
  6.  
  7. app = Flask(__name__)
  8.  
  9. registered = False
  10.  
  11. uname = " "
  12. pwd = " "
  13.  
  14. @app.route('/',methods=['GET', 'POST'])
  15. def index():
  16. if request.method == 'GET':
  17. return render_template('MainPage.html')
  18. else:
  19. db = pymysql.connect('127.0.0.1', 'root', '123', 'username and passwords', port=3306)
  20.  
  21. cursor = db.cursor()
  22.  
  23. select = """SELECT * FROM `username and passwords`.`userpwd` LIMIT 1000;"""
  24.  
  25. cursor.execute(select)
  26.  
  27. db.close()
  28.  
  29. l = cursor.fetchmany(100)
  30. for i in l:
  31. global uname
  32. global pwd
  33. uname = i[0]
  34. pwd = i[1]
  35.  
  36. if request.form['username'] == uname and hashlib.sha512(str(request.form['password']).encode()).hexdigest() == pwd:
  37. registered = True
  38. return 'Logged in as admin'
  39. else:
  40. return 'Incorrect username or password'
  41.  
  42. @app.route('/register',methods=['GET', 'POST'])
  43. def register():
  44. if request.method == 'GET':
  45. return render_template('Register.html')
  46. else:
  47. username = request.form['username']
  48. password = hashlib.sha512(str(request.form['password']).encode()).hexdigest()
  49.  
  50. db = pymysql.connect('127.0.0.1', 'root', '123', 'username and passwords', port=3306)
  51. cursor = db.cursor()
  52. insert = """INSERT INTO `username and passwords`.`userpwd` (`USERNAME`, `PASSWORD`)
  53. VALUES (%s,%s);""" % ('\'' + username + '\'', '\'' + str(password) + '\'')
  54. cursor.execute(insert)
  55. db.commit()
  56. db.close()
  57. return redirect(url_for('index'))
  58.  
  59. @app.route('/blog',methods=['GET', 'POST'])
  60. def Blog():
  61. if request.method == 'GET':
  62. return render_template('Blog.html')
  63. else:
  64. return 'You need to register'
  65.  
  66. @app.route('/contact',methods=['GET', 'POST'])
  67. def Contact():
  68. if request.method == 'GET':
  69. return render_template('Contact.html')
  70. else:
  71. return ''
  72.  
  73. @app.route('/about',methods=['GET', 'POST'])
  74. def About():
  75. if request.method == 'GET':
  76. return render_template('About.html')
  77. else:
  78. return ''
  79.  
  80. if __name__ == '__main__':
  81. app.config['SECRET_KEY'] = "Your_secret_string"
  82. app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement