00001
00002 '''
00003 This module provides basic funcitons for the client_classes.py and start_client.py.
00004
00005 Reading University
00006 MSc in Network Centered Computing
00007 a.weise - a.weise@reading.ac.uk - December 2005
00008 '''
00009
00010 import ConfigParser, string, os, sys, termios
00011
00012 def LoadConfig(file_name, config={}):
00013 """
00014 returns a dictionary with key's of the form
00015 <section>.<option> and the values
00016
00017 source: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65334
00018 """
00019 config = config.copy()
00020 cp = ConfigParser.ConfigParser()
00021 cp.read(file_name)
00022 for sec in cp.sections():
00023 name = string.lower(sec)
00024 for opt in cp.options(sec):
00025 config[name + "." + string.lower(opt)] = string.strip(cp.get(sec, opt))
00026 return config
00027
00028 def check_ip(ip):
00029 '''
00030 This function check if a given IP is valid.
00031 '''
00032 try:
00033 ip = ip.split(".")
00034 except AttributeError:
00035 return -1
00036
00037 for i in range(len(ip)):
00038 check = ip[i].find("0", 0, 1)
00039 if -1 != check and 1 < len(ip[i]):
00040 return -1
00041 try:
00042 ip[i] = int(ip[i])
00043 except ValueError:
00044 return -1
00045 if ip[i] >= 0 and ip[i] <= 255:
00046 pass
00047 else:
00048 return -1
00049
00050 return 0
00051
00052 def usage_exit(progname, msg=None):
00053 '''
00054 This function gives usage help and exits script.
00055 '''
00056 if msg:
00057 print msg
00058 print
00059 print "usage: python %s [ -h|--help -c|--config -p|--smtp_passord -v|--verbose -d|--daemon] \n\n" % progname
00060 os._exit(-1)
00061
00062 def get_password(msg):
00063 '''
00064 This function reads from stdin without echoing the input.
00065
00066 source: http://gnu.kookel.org/ftp/www.python.org/doc/faq/library.html
00067 modified by a. weise December 2005
00068 '''
00069 fd = sys.stdin.fileno()
00070
00071 old = termios.tcgetattr(fd)
00072 new = termios.tcgetattr(fd)
00073 new[3] = new[3] & ~termios.ICANON & ~termios.ECHO
00074 new[6][termios.VMIN] = 1
00075 new[6][termios.VTIME] = 0
00076 termios.tcsetattr(fd, termios.TCSANOW, new)
00077 s = ''
00078 try:
00079 print
00080 print msg
00081 while 1:
00082 c = os.read(fd, 1)
00083 if c == "\n":
00084 break
00085 s = s+c
00086 finally:
00087
00088 termios.tcsetattr(fd, termios.TCSAFLUSH, old)
00089 return s