Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask, render_template,request,redirect
- app = Flask(__name__)
- users = {} # this holds all the users
- @app.route('/create/', methods = ['post', 'get'])
- def create():
- global users
- if request.method == 'POST':
- #now get the name/username & password to create the account
- name = request.form['name'] # get the name
- userName = request.form['user_name'] # get User name
- password = request.form['password'] # get pass
- # check to see if this user_name exists because they must be unique!
- if users.has_key(userName):
- print "error key already exists"
- return render_template("create.html", responsetext = "User Name already taken :(")
- else:
- print "Key successfully created! "
- writeKey(users,name,userName,password) # write the key to the dictionary and to the file
- return "Account created!"
- return render_template("create.html")
- @app.route('/', methods = ['post','get'])
- def login_page():
- global users
- #check to see if the users dictionary is empty, if it is read it in from the file
- if not bool(users): # if its empty, read it in from the file
- print "Reading in from the file"
- readUsers(users)
- # If the user sends back a request( either log in or create a new account)
- if request.method == 'POST':
- # ck which type of response it was
- if request.form["submit"] == "log_in": # user is trying to log into his/her account
- print request.form["user_name"] #print to the console for debugging purposes
- userName = request.form["user_name"]
- password = request.form["password"]
- # ck the credentials
- if checkCredentials(userName,password): #verification was a success
- return 'Logged in successfully :D'
- else:
- print "failed"
- return render_template("login.html", responsetext="You entered a invalid username/password")
- elif request.form["submit"] == "create_account":
- return redirect("create/") # redirect to the users page
- return render_template("login.html")
- def readUsers(users):
- # so read the file and store all the users in the dictionary
- file = open("users.txt","r") #open file for only reading
- for line in file:
- string = line.split(':') # split the line based on colon (:). (name userId )
- i = 0
- while i < len(string): #loop through the file and read in the users
- name = string[i]
- i+=1
- user_name = string[i]
- i+= 1
- password = string[i]
- i+=1
- users[user_name] = (name,password)
- print (users)
- file.close()
- def writeKey(users,name,userName,password):
- users[userName] = (name,password) #insert into the dictionary
- file = open("users.txt","a") #write to the file
- line = name + ":"+userName+":"+password+"\n"
- file.write(line)
- file.close()
- def checkCredentials(userName,typedPass):
- global users
- if users.has_key(userName):
- (name,userPass) = users[userName]
- print "typedPass " + typedPass
- print "savedPass " + userPass
- print typedPass is typedPass
- return (typedPass == userPass)
- else:
- return False
- if __name__ == '__main__':
- app.run("127.0.0.1",1300,debug = True);
- # with app.test_request_context():
- # print url_for('login')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement