last python question; code dificulties

  • From: "Tyler Littlefield" <tyler@xxxxxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Fri, 3 Apr 2009 12:52:10 -0600

Hello list,
Sorry for all the questions.
I've got my dictionary issue solved, (thanks to those who helped with that), 
and I'm trying to get my playerfiles set up. I have to type in my name at login 
twice, and it finally creates my player file, but it's got a bunch of hunk in 
the name.
If someone wouldn't mind taking a look at this, I'd really really appriciate it.
I've just pasted the code below:
#code:
from ConfigParser import ConfigParser
import os
import string
import sha

class user:
 '''
 holds the base functions for handling user data
 '''
#constructer:
 def __init__(self,sock):
  self.connection=sock
  self.vars={}
  self.parser=None 
  self.state=0

 #will send data to the user:
 def tell(self,message):
  '''
  sends data to the users socket.
  '''
  self.connection.send(str(message)+"\r\n")
  
  #will remove the user from the sockets list and close:
 def kill(self):
  '''
  removes the user, closes it's socket and removes the class from the users 
list.
  '''
  global sockets
  global users
  for i in sockets:
   if (i==self.connection):
    i.close()
    sockets.remove(i)
    users.remove(self)

 def WriteFile(self,file):
  '''
  writes the users data to a file.
  '''
  fd=open("players/"+file,"w")
  cf=ConfigParser()
  cf.add_section("player")
  for key,value in self.vars.items():
   cf.set("player",key,value)
  cf.write(fd)
  fd.flush()
  fd.close()
  return 1
  
 def LoadFile(self,file):
  '''
  loads the players file in to the vars dictionary.
  '''
  cf=ConfigParser()
  cf.read(file)
  self.vars=cf.items("player")
 
 def login(self,data):
  '''
  the function that is called to parse login commands
  '''
  input=string.strip(data,string.digits+string.punctuation+string.whitespace)
  print input
  if (self.state==0):
   #we print the banner (login text) to the user
   file=open("txt/welcome","r")
   welcome=file.read()
   file.close()
   self.tell(welcome)
   self.tell("What is your name?")
   self.flush()
   self.state+=1
   self.parser("")
   return
  if ((self.state==1) and (len(input)>0)):
  #accept the users name, and see if it exists
   if (self.UserExists(input)):
    vars['name']=input
    self.state+=1
    return
   else:
    self.CreateUser(input)
    self.parser=self.CreateUser
    self.state=10
   return
  self.tell("I don't understand that")
  return
  
 def UserExists(self,data):
  dir=os.getcwd()
  dir+="/players/"
  dirs=os.listdir(dir)
  for d in dirs:
   if (string.lower(d)==string.lower(data)):
    return True
  return False

 def CreateUser(self,data):
  '''
  data is the name selected.
  '''
  if (self.state==10):
   #get password
   self.tell("What is your password?")
   self.vars['name']=data 
   self.state+=1 
   return
  if (self.state==11):
  #password was supplied
   
password=string.strip(data,string.digits+string.punctuation+string.whitespace)
   if (len(password)==0):
    self.tell("An invalid password was supplied.")
    return
   password=self.encrypt(password)
   print password
   self.vars['password']=password
   self.WriteFile(self.vars['name']) 
   self.kill()

 def encrypt(self,data):
  sh=sha.sha()
  sh.update(data)
  return sh.digest()

 def flush(self):
  self.tell("\r\n")
  #end code


Thanks,
Tyler Littlefield
Web: tysdomain.com
email: tyler@xxxxxxxxxxxxx
My programs don't have bugs, they're called randomly added features.

Other related posts:

  • » last python question; code dificulties - Tyler Littlefield