Show DevBest User login and register script

Status
Not open for further replies.

Pro123

New Member
Feb 20, 2011
21
0
Code:
print 'You have successfully logged in!'

Code:
#Published by Pro123
import hashlib, time

#User database, updated on runtime
database =  {}

# Returns an encrypted string
def encrypt(string):
    crypt = hashlib.sha512()
    crypt.update(string)
    return crypt.hexdigest()

#First update database with a complex file iteration
def update():
    global database
    file = open('data.txt')
    y = file.readlines()
    for x in range(len(y)):
        try:
            if x == 1:
                strip = str(y[0])
                strip = strip.replace('\n', '')
                strip = strip.split(':')
                database[strip[0]]=strip[1]
                if y[1]:
                    strip = str(y[1])
                    strip = strip.replace('\n', '')
                    strip = strip.split(':')
                    database[strip[0]]=strip[1]
                else:
                    pass
            else:
                strip = str(y[x])
                strip = strip.replace('\n', '')
                strip = strip.split(':')
                database[strip[0]]=strip[1]
        except (IndexError):
            f = open('data.txt', 'w')
            f.write('')
            menu()
            f.close()
        file.close()

#Basic menu
def menu():
    print 'Welcome to the Main Page\n'
    print '1.) Login\n'
    print '2.) Register\n'
    choice = int(raw_input('What would you like to do?(1 or 2):'))
    if choice == 1:
        login()
    elif choice == 2:
        register()
    else:
        print 'You have entered an incorrect choice'
        time.sleep(1)
        menu()
    

#Login function, add your own function after the "You have successfully logged in" message to implement
def login():
    global database
    print '\n Hello, welcome to the login page\n'
    user = str(raw_input('Please enter your username:\n'))
    user = encrypt(user)
    if database.has_key(user):
        password = str(raw_input('\nHello, please enter your password:' ))
        password = encrypt(password)
        if database[user] == password:
            print 'You have successfully logged in!' 
        else:
            print 'You have entered an incorrect password, choose one of the following'
            print '\n 1.) Try again'
            print '\n 2.) Return to the main menu'
            choice = int(raw_input('What would you like to do?(1 or 2):'))
            if choice == 1:
                login()
            elif choice == 2:
                menu()
            else:
                print 'You have entered an incorrect answer, returning you to the menu...\n'
                time.sleep(1)
    else:
        print 'You have entered a non existant username, choose one of the following:\n'
        print '1.) Try again'
        print '2.) Register'
        print '3.) Return to the main menu'
        choice = int(raw_input('What would you like to do?(1, 2 or 3):'))
        if choice == 1:
            login()
        elif choice == 2:
            register()
        elif choice == 3:
            menu()
        else:
            print 'You have entered an incorrect choice, returning you to the menu...'
            time.sleep(1)
            menu()

#Register function
def register():
    global database
    print '\n Welcome to the registeration page!\n'
    user = str(raw_input('Please enter a new username:'))
    user = encrypt(user)
    if database.has_key(user):
        print 'User name exists, please enter a new one\n'
        register()
    elif user == '':
        print 'Invalid Username...\n'
        register()
    else:
        password = str(raw_input('Please create a new password for your account:'))
        if password == '':
            print 'Invalid password'
            register()
        else:
            password = encrypt(password)
            database[user] = password
            f = open('data.txt', 'a+')
            string = user + ':' + password + '\n'
            f.write(string)
            f.close()
            print 'You have successfully created your account!, what would you like to do?'
            print '\n 1.) Return to the menu'
            print '\n 2.) Login to your account'
            choice = int(raw_input('What would you like to do?(1 or 2):'))
            if choice == 1:
                    menu()
            elif choice == 2:
                login()
            else:
                print 'You have typed an incorrect answer, returning you to the menu...'
                time.sleep(1)
                menu()
        
if __name__ == '__main__':
    if open('data.txt').read() == '':
        menu()
    else:
        update()
        menu()
 

Pro123

New Member
Feb 20, 2011
21
0
wel its python and this is not an tutorials its an release of some scripts of python i made
 

Macemore

Circumcised pineapples
Aug 26, 2011
1,681
819
I've never used python before, so I don't know where do put my MySQL database info into.
and I don't know what file extension to use?
 
Status
Not open for further replies.

Users who are viewing this thread

Top