Advertisement
Guest User

Untitled

a guest
Feb 15th, 2016
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. from flask import Flask, render_template,request,redirect
  2. app = Flask(__name__)
  3.  
  4. users = {} # this holds all the users
  5.  
  6.  
  7. @app.route('/create/', methods = ['post', 'get'])
  8. def create():
  9. global users
  10. if request.method == 'POST':
  11. #now get the name/username & password to create the account
  12. name = request.form['name'] # get the name
  13. userName = request.form['user_name'] # get User name
  14. password = request.form['password'] # get pass
  15. # check to see if this user_name exists because they must be unique!
  16. if users.has_key(userName):
  17. print "error key already exists"
  18. return render_template("create.html", responsetext = "User Name already taken :(")
  19. else:
  20. print "Key successfully created! "
  21. writeKey(users,name,userName,password) # write the key to the dictionary and to the file
  22. return "Account created!"
  23.  
  24. return render_template("create.html")
  25.  
  26.  
  27. @app.route('/', methods = ['post','get'])
  28. def login_page():
  29. global users
  30. #check to see if the users dictionary is empty, if it is read it in from the file
  31. if not bool(users): # if its empty, read it in from the file
  32. print "Reading in from the file"
  33. readUsers(users)
  34. # If the user sends back a request( either log in or create a new account)
  35. if request.method == 'POST':
  36. # ck which type of response it was
  37. if request.form["submit"] == "log_in": # user is trying to log into his/her account
  38. print request.form["user_name"] #print to the console for debugging purposes
  39. userName = request.form["user_name"]
  40. password = request.form["password"]
  41. # ck the credentials
  42. if checkCredentials(userName,password): #verification was a success
  43. return 'Logged in successfully :D'
  44. else:
  45. print "failed"
  46. return render_template("login.html", responsetext="You entered a invalid username/password")
  47.  
  48.  
  49. elif request.form["submit"] == "create_account":
  50. return redirect("create/") # redirect to the users page
  51. return render_template("login.html")
  52.  
  53.  
  54.  
  55. def readUsers(users):
  56. # so read the file and store all the users in the dictionary
  57. file = open("users.txt","r") #open file for only reading
  58. for line in file:
  59. string = line.split(':') # split the line based on colon (:). (name userId )
  60. i = 0
  61. while i < len(string): #loop through the file and read in the users
  62. name = string[i]
  63. i+=1
  64. user_name = string[i]
  65. i+= 1
  66. password = string[i]
  67. i+=1
  68. users[user_name] = (name,password)
  69.  
  70. print (users)
  71. file.close()
  72.  
  73.  
  74. def writeKey(users,name,userName,password):
  75. users[userName] = (name,password) #insert into the dictionary
  76. file = open("users.txt","a") #write to the file
  77. line = name + ":"+userName+":"+password+"\n"
  78. file.write(line)
  79. file.close()
  80.  
  81.  
  82. def checkCredentials(userName,typedPass):
  83. global users
  84. if users.has_key(userName):
  85.  
  86. (name,userPass) = users[userName]
  87. print "typedPass " + typedPass
  88. print "savedPass " + userPass
  89. print typedPass is typedPass
  90. return (typedPass == userPass)
  91. else:
  92. return False
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102. if __name__ == '__main__':
  103. app.run("127.0.0.1",1300,debug = True);
  104.  
  105. # with app.test_request_context():
  106. # print url_for('login')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement