The Sorrow Posted December 4, 2010 Posted December 4, 2010 Im trying to figure out how to import user names and passwords into a list in python from a text file. Something like this: List = "" list = [open(*.txt)] can i get some direction? Quote
Infiltrator Posted December 4, 2010 Posted December 4, 2010 Does the text file already contain all the usernames and passwords or just the passwords. Quote
The Sorrow Posted December 4, 2010 Author Posted December 4, 2010 i was planning on making separate files, one holding user names the other holding passwords. I'm at work and cant show you my source atm. Quote
Infiltrator Posted December 4, 2010 Posted December 4, 2010 My suggestion would probably be to create two separate arrays, one for storing the "Usernames[]" and the passwords "Passwords[]" values as it reads the text files. And then have two "for loops" for each corresponding array for writing the credentials back to file. Don't know If that makes sense to you. Quote
The Sorrow Posted December 4, 2010 Author Posted December 4, 2010 It makes sense... Get back to me when i can post my code. Quote
The Sorrow Posted December 4, 2010 Author Posted December 4, 2010 username = " " password = " " usernameList = [] passwordList = [] login = "n" indexer = 0 while (login == "n"): print "Please enter your Username" username = str.lower(raw_input("> ")) str.lower(username) indexer = usernameList.index(username) print "Please enter your Password" password = raw_input("> ") if (username == usernameList[indexer] and password == passwordList[indexer]): print "Login Successful!" login = "y" else: print "Login Failed, Please try again" Here is what i have thus far. I just need to import the lists into the arrays from text files Quote
Alias Posted December 5, 2010 Posted December 5, 2010 Here we go, just a little something that does the trick. It reads from a file called "passwd" in the current directory. The formatting of the logins is "username:password" it will parse those and then put them into a dictionary. This has benefits because it will automatically expand to however many logins there are in the file. I also took out all of the lowering of strings, it's unnecessary and a security risk. I've also added some basic SHA encryption as well. An already SHA encrypted password is placed in the login file instead of a cleartext password. Then the program computes the SHA hash of the user given password. If the two of them match then the user is logged in. There might be a few bugs you run into, for example if the "username:password" string isn't formatted correctly in the password file you may have some problems. Overall it's fairly easy to follow but if you've got any questions just ask. Here's my passwd file there are 2 users, a normal user and a guest user, the user Alias has a private password (:P) and the guest user has a password of "password" Alias:2dd00738ed848e5fad7070ef9104c62c2dfe82ab51e3ef6e1bb81bd4caecad63 guest:5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8 import string as str import hashlib as crypto username = "" password = "" login = "n" logins = dict() filePointer = open("passwd", "r") for line in filePointer: line = str.strip(line, "\n") loginInfo = str.split(line, ":", 2) logins[loginInfo[0]] = loginInfo[1] while (login == "n"): print "Please enter your Username" username = raw_input("> ") print "Please enter your Password" password = crypto.sha256(raw_input("> ")).hexdigest() if(logins[username] == password): login = "y" print "Login Successful!" else: print "Login Failed\n" Quote
P@c_M@n Posted December 10, 2010 Posted December 10, 2010 As Alias did, if you are trying to set up an authentication system with python, you should use hashlib so that if someone finds the file with the usernames and passwords, they can't just have instant access to your system. Here is a topic explaining it more in depth. intro to cryptography Quote
Alias Posted December 10, 2010 Posted December 10, 2010 As Alias did, if you are trying to set up an authentication system with python, you should use hashlib so that if someone finds the file with the usernames and passwords, they can't just have instant access to your system. Here is a topic explaining it more in depth. intro to cryptography That's actually a really good link although it doesn't explain the cryptography to the extent that it should. Still a good read though. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.