Example #1
0
def main():
    global username
    global password
    global listenPort
    global tempDir
    global execDir
    global directoryList
    global extensionList
    global commandHandle
    global debugHandle
    global debugEnable

    signal.signal(signal.SIGTERM, sigHandler)

    try:
        cfgfile=sys.path[0]+"/stm.cfg"
        if sys.platform=='win32': cfgfile=cfgfile.replace('/','\\')

        config=ConfigParser.RawConfigParser()
        config.read(cfgfile)

        if config.has_option('ServeToMe','username'): username=config.get('ServeToMe','username')
        if config.has_option('ServeToMe','password'): password=config.get('ServeToMe','password')
        if config.has_option('ServeToMe','listenPort'): listenPort=config.getint('ServeToMe','listenPort')
        tempDir=config.get('ServeToMe','tempDir')
        execDir=config.get('ServeToMe','execDir')
        directoryList=config.get('ServeToMe','directoryList').split(',')
        if config.has_option('ServeToMe','extensionList'): extensionList=tuple(config.get('ServeToMe','extensionList').split(','))
        if config.has_option('ServeToMe','debugEnable'): debugEnable=config.getboolean('ServeToMe','debugEnable')
    except:
        print "Configuration File Error\n"
        sys.exit(2)

    if debugEnable:
        if commandHandle is None: commandHandle=open(tempDir+"/command.log","w")
        if debugHandle is None: debugHandle=open(tempDir+"/debug.log","w")
        #sys.stderr = debugHandle
    else:
        sys.stderr = open(os.path.devnull, 'a+', 0)

    debugLog("Welcome to ServeToMe ({0})".format(STM_VERSION))
    debugLog("====================")

    debugLog("username={0}".format(username))
    debugLog("password={0}".format(password))
    debugLog("listenPort={0}".format(listenPort))
    debugLog("tempDir={0}".format(tempDir))
    debugLog("execDir={0}".format(execDir))
    debugLog("directoryList={0}".format(directoryList))
    debugLog("extensionList={0}".format(extensionList))
    debugLog("debugEnable={0}".format(debugEnable))

    try:
        server = HTTPServer(('', listenPort), requestHandler)
        itimer = RepeatTimer(SESSION_TIMEOUT_STEP, sessionIdle)
        itimer.start()
        server.protocol_version='HTTP/1.1'
        debugLog('started httpserver...')
        server.serve_forever()
    except KeyboardInterrupt:
        debugLog('Termination signal received, shutting down server')
        itimer.cancel()
        server.socket.close()
        while len(sessions)>0:
            for client in sessions:
                sessionKill(client)
                break
        # Close debug command log
        if not commandHandle is None: commandHandle.close()
        if not debugHandle is None: debugHandle.close()