critique my 1st program ever

pehr anderson pehr at pehr.net
Thu Jul 20 00:29:26 EDT 2000


1. Linux gives your local IP address bound to an interface. 
  My machine has several IP addresses, one for each inteface. 
  Some servers have more than one IP address per interface!
  Your problem is not as easy as it sounds.
  The dirty way is to run /sbin/ipconfig and parse the result.
	txt = os.popen("/sbin/ipconfig").read()

2. Making a background process start transparently 
  on multiple operating systems is going to be a challenge.
  Linux has cron for regular tasks and the /etc/rc.d/init
  directory structure for boot scripts.  Linuxconf does
  a good job of managing thes start/stop scripts if you 
  just follow the existing command-line conventions.

  I have no idea how to install a windows service.
  You may find some pointers in the "Pyhton for Win32" book.

3. To see platform-dependent stuff:
	import sys; print sys.platform
	on my box I get linux-i386


jtoy wrote:
> 
> 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