Thursday, May 24, 2012

My first Python Program

Today i started learning Python. I had first program. After finish, i learned:
print number, string, format etc
function
local and global variable
read file
string find index and substring

And now, this is my first program:
#Author:     Namhb
#Email:     namhb@gmail.com
#Program:    Demo1
#Version:    0.1

#############################################
##### Start program

##### Lib
import getpass
##### Global Variable
userfile = "user.txt"
userc = ""
rolec = ""
logined = False
failcount = 0
maxfail = 3
##### Function
def menu():
    print "+---------------------------------------------+"
    print "+                                             +"
    print "+     Demo Python Program                     +"
    print "+   namhb@gmail.com                           +"
    print "+                                             +"
    print "+---------------------------------------------+"

def checklogin(user, passwd):
    f = file(userfile)
    check = False
    while True:
        line = f.readline()
        if len(line) == 0:
            break
        u_end = line.index(":")
        u = line[0:u_end]
        p_end = line.index(":",u_end+1)
        p = line[u_end+1:p_end]
        r = line[p_end+1:len(line)-1]
        if user == u and passwd == p :
            check = True   
            break
    if check == True:
        # Set cookie
        global userc, rolec, logined
        userc = user
        rolec = r
        logined = True
    f.close()

def login():
    print "Login"
    username = raw_input("Username: ")
    password = getpass.getpass()
    print "Loading: user %s with password in %s" %(username,userfile)
    checklogin(username, password)
    if logined == True:
        print "Login successfully"
        print "Hello %s - %s" %(userc, rolec)
    else:
        global failcount
        failcount += 1
        print "Login failed, you have %d to try again" %(maxfail - failcount)
        if (maxfail - failcount) > 0:
            login()
        else:
            exit()       
   
##### Main
menu()
login()

And user.txt file:
namhb:123456:admin
linh:123456:user

------------------------------------------------------------
Thanks for reading
--------------------------------------------------------------------------
Security Research
All my Lab:
Linux Lab -- window and Cisco Lab
to be continued - I will update more.

No comments:

Install Xposed Inspector and Frida on Genymotion

Today i had some work with android. So i need trace application. I found 2 nice tool can help me: Xposed Inspector and Frida. To setup ther...