#!/usr/bin/python import cgi # Supports writing to the info.dat file. This cgi # script gets parameter data from an XPort or other # embedded device, and writes it out to the info.dat # file, so long as the 'user' parameter is in the # allowed.dat file def isAllowed(user=None): """Checks to see that the given user name is in the allowed.dat file""" if user == None: return False allowedFile = open("allowed.dat", 'r') allowed = [] for name in allowedFile.readlines(): allowed.append(name.strip()) allowedFile.close() if user in allowed: return True else: return False def writeMessage(user=None, message="Default"): """Writes a message to the info.dat file, with the parameters user:time:message""" # read in the whole info.dat file userInfoFile = open("info.dat", 'r') userInfo = [] for line in userInfoFile.readlines(): parts = line.split(':') if len(parts) == 2: userInfo.append(parts) userInfoFile.close() # parse out the entries to find the appropriate one written = False for datum in userInfo: if datum[0] == user: datum[1] = message written = True if not written: userInfo.append([user, message]) # replace the entry with the new value userInfoFile = open("info.dat", 'w') for datum in userInfo: if len(datum) == 2: userInfoFile.write("%s:%s\n" % (datum[0], datum[1].strip())) userInfoFile.close() params = cgi.FieldStorage() print "Content-type: text/html\n\n" user = None message = None if params.has_key("user"): user = params["user"].value if params.has_key("msg"): message = params["msg"].value if isAllowed(user): # write the data writeMessage(user, message) print "ok" else: print "not ok"