critique my 1st program ever

jtoy jtoy at tcgfinancial.com
Wed Jul 19 17:37:46 EDT 2000


Hi, this is my first program I have ever written.  Please tell e what I
need to do to clean it up or make any other suggestions.  Thanks.
Also, could you answer a couple of question I have below.

# Written by Jason Toy(toyboy at toy.eyep.net)http://toy.eyep.net on 02:07
July 16, 2000 EST
#This program is an eyep client for any OS that has Python installed
import urllib, os, string

def parser(host, userdata):
  if userdata['ip'] =="":
    eyep =
'http://eyep.net/update.php/'+userdata['username']+'/'+userdata['password']+'/'+host

  else:
    eyep =
'http://eyep.net/update.php/'+userdata['username']+'/'+userdata['password']+'/'+host+'/'+userdata['ip']

  parsereturn = urllib.urlopen(eyep)
  if parsereturn.read() == "RETURN4":
    print host, "has been updated successfully"
  elif parsereturn.read() == "RETURN1":
    print "You are missing fields, request is probably misconfigured
for", host
  elif parsereturn.read() == "RETURN2":
    print "Bad username/password for", host
  elif parsereturn.read() == "RETURN3":
    print host, "isn't owned by you"
  elif parsereturn.read() == "RETURN5":
    print "Invalid IP address for", host
  elif parsereturn.read() == "RETURN6":
    print "Service is temporarily unavailable"
  else:
    print "Error message on", host, ":", parsereturn.read() ,"--Please
report to http://www.eyep.net for further information."

def readuserdata(aFileName):
  userdata = {}
  myfile = open(aFileName)
  for line in myfile.readlines():
    (item, value) = string.split(line, ':', 1)
    userdata[item] = string.strip(value)
  return userdata

def writeuserdata(aFileName, userdata):
  myfile = open(aFileName, 'w')
  for item in userdata.keys():
    myfile.write("%s:%s\n"% (item, userdata[item]))
  myfile.close()
userdata = {}
if os.access('userdata.dat', os.R_OK) == 0:
  userdata['username'] = raw_input("Enter your user name: ")
  userdata['password'] = raw_input("Enter your password: ")
  userdata['host1'] = raw_input("Enter you first hostname: ")
  userdata['host2'] = raw_input("Enter your second host(if you have only
1, press enter): ")
  if userdata['host2'] != "":
    userdata['host3'] = raw_input("Enter your third host(If you have
only 2, press enter): ")
  userdata['ip'] = raw_input("Enter your ip(press enter for
autodetection): ")

else:
  userdata = readuserdata('userdata.dat')
parser(userdata['host1'], userdata)
if userdata['host2'] !="":
  parser(userdata['host2'], userdata)
  if userdata['host3'] !="":
    parser(userdata['host3'], userdata)
else:
  print "Thank You"
if os.access('userdata.dat', os.R_OK) == 0:
  print "Saving your configuration........."
  writeuserdata('userdata.dat', userdata)

Can you tell me how I can make a python program find the IP address of
the local machine?  If possible, I would like to make this not matter
what OS its on( i want it to work on windows and Unix/Linux/BSD).
Second, can you tell me how I could run this program as a dameon, again
not OS dependent, because while I know you can put a Python script in
cron,  how can you do the same with windows?
Third, how can I make Python recognize what OS its on, that wat if I
can't do the above 2 questions withouot being OS dependent, then I will
do somethin like: if OS == x then do else OS == z do
Thanks for your time.

Jason Toy
toyboy at toy.eyep.net
http://toy.eyep.net




More information about the Python-list mailing list