pickle.load not working?
sp1d3rx at gmail.com
sp1d3rx at gmail.com
Fri Aug 12 18:03:00 EDT 2005
I have the following code for controlling access:
----------------------
#check login credentials...
def cklogin(ipaddy, user, authcoded):
try:
print "Opening file:", user
f = file(user,'r+') #load in the user from the file
cusr = pickle.load(f)
print "User Authcode:", cusr.authcode
print "Supplied:", authcoded
print "Login:", cusr.login
if cusr.authcode == authcoded:
print "User", user, "successfully logged in."
cusr.loggedin=TRUE
cusr.invalid_logins = 0
cusr.ip = ipaddy
pickle.dump(cusr, f) # save the new status...
del cusr
f.close()
return mkcookie(ipaddy)
else:
print "Invalid login attempt."
del cusr
f.close()
except IOError:
print "User does not exist!"
return FALSE
----------------------
it reads a file saved this way:
----------------------
import pickle
class chatuser: #container for storing user information...
login = ""
authcode = ""
cookie = ""
ip = ""
loggedin = 0
invalid_logins = 0
allow_login = 1
status = ""
realname = ""
phone = ""
email = ""
derek = chatuser
derek.login = "username"
derek.authcode = "password"
derek.cookie = "123456"
derek.ip = "127.0.0.1"
derek.loggedin = 0
derek.invalid_logins = 0
derek.allow_login = 1
derek.status = "here"
derek.realname = "Derek W."
derek.phone = "480-XXX-XXXX"
derek.email = "XXX.YYY at BBB.com"
f = file(derek.login, 'w')
pickle.dump(derek, f)
f.close()
----------------------
When I try to read it via the interpreter, it works like this...
----------------------
>>> import pickle
>>> f = file("spiderx", 'r')
>>> c = pickle.load(f)
>>> c
<class __main__.chatuser at 0x00F1B600>
>>> c.login
'username'
>>> c.authcode
'password'
----------------------
With the code above, the login and authcode fields are blank... so, I
don't understand why it doesn't work. Is there anything wrong with my
code?
More information about the Python-list
mailing list