Exemplo n.º 1
0
def socketRev(connection, addr):

    data = rev = ''
    i = 0
    j = 0
    while True:
        data = connection.recv(SOCKET['BUFSIZE'])
        if data == '':
            i = i + 1
        rev = rev + data
        if data.endswith('EOF') or addr[
                0] == '127.0.0.1' or rev == 'PORT_IS_ALIVE' or i > 2 or j > 10:
            break
        time.sleep(round(float(random.randrange(0, 10000, 1)) / 10000, 4))
        j = j + 1

    if rev == 'PORT_IS_ALIVE':
        connection.close()
        sys.exit()

    if addr[0] == '127.0.0.1':
        return rev
    else:
        try:
            rev = decode(rev)
        except Exception as e:
            try:
                time.sleep(
                    round(float(random.randrange(0, 10000, 1)) / 10000, 4))
                rev = decode(rev + connection.recv(SOCKET['BUFSIZE']))
            except Exception as e:
                save_log('ERROR', 'server cmd decode error:' + str(e))

    if rev.isspace() or not rev:
        save_log('WARNING', 'Empty command.')
        connection.send(encode('Illegal Command '))
        connection.close()
        sys.exit()

    return rev
Exemplo n.º 2
0
def socketRev(connection, addr):

    data = rev = ""
    i = 0
    j = 0
    while True:
        data = connection.recv(SOCKET["BUFSIZE"])
        if data == "":
            i = i + 1
        rev = rev + data
        if data.endswith("EOF") or addr[0] == "127.0.0.1" or rev == "PORT_IS_ALIVE" or i > 2 or j > 10:
            break
        time.sleep(round(float(random.randrange(0, 10000, 1)) / 10000, 4))
        j = j + 1

    if rev == "PORT_IS_ALIVE":
        connection.close()
        sys.exit()

    if addr[0] == "127.0.0.1":
        return rev
    else:
        try:
            rev = decode(rev)
        except Exception as e:
            try:
                time.sleep(round(float(random.randrange(0, 10000, 1)) / 10000, 4))
                rev = decode(rev + connection.recv(SOCKET["BUFSIZE"]))
            except Exception as e:
                save_log("ERROR", "server cmd decode error:" + str(e))

    if rev.isspace() or not rev:
        save_log("WARNING", "Empty command.")
        connection.send(encode("Illegal Command "))
        connection.close()
        sys.exit()

    return rev
Exemplo n.º 3
0
def socketDo(connection, addr):
    '''
	server指令处理模块
	@connection: 指令连接对象
	'''

    try:

        rev = socketRev(connection, addr)

        if isConseResult(rev):
            connection.send(encode('result_send_ok'))
        #处理从unctrlpy 传过来的结果信息
        try:
            chooseResultDef(rev)
        except Exception as e:
            save_log('ERROR', 'chooseResultDef error:' + str(e))

        cmdstr, ip = ayCmdToConsole(rev)

        if not ip:
            save_log('ERROR', 'IP IS NULL')
            connection.send('Ip is null.')
            connection.close()
            sys.exit()

        save_log('INFO', str(cmdstr.split('!')))

        # if cmd for myself
        cname = isMySelfCmd(cmdstr)
        #if batch cmd
        batch = isBatchCmd(cmdstr)
        toPhpData = ""
        if cname:
            try:
                toPhpData = OsaChooseDef(defname=isMySelfCmd(cmdstr),
                                         ip=ip,
                                         fromPhpCmd=cmdstr)
            except Exception as e:
                save_log('ERROR', e)
        elif batch:
            toPhpData = 'BATCH_CMD_OK'
        else:
            toPhpData = hostSocket.proSocket(ip, SOCKET['REMOTE_PORT'], cmdstr)
            save_log('DEBUG', 'cmd info:' + str(encode(cmdstr)))
            save_log('DEBUG', 'cmd info:' + str(cmdstr))

        if toPhpData == False:
            toPhpData = 'Data_receive_Failed!'
        connection.send('%s!%s' % (toPhpData, ip))
        connection.close()
        if toPhpData == 'BATCH_CMD_OK':

            try:
                chooseBatchDef(cmdstr, rev)
            except Exception as e:
                save_log('ERROR', 'BATCH chooseBatchDef error:' + str(e))
        sys.exit()

    except queueEmptyException, value:
        save_log('ERROR', 'server socketDo:' + str(value))
        connection.close()
        sys.exit()
Exemplo n.º 4
0
def socketDo(connection, addr):
    """
	server指令处理模块
	@connection: 指令连接对象
	"""

    try:

        rev = socketRev(connection, addr)

        if isConseResult(rev):
            connection.send(encode("result_send_ok"))
            # 处理从unctrlpy 传过来的结果信息
        try:
            chooseResultDef(rev)
        except Exception as e:
            save_log("ERROR", "chooseResultDef error:" + str(e))

        cmdstr, ip = ayCmdToConsole(rev)

        if not ip:
            save_log("ERROR", "IP IS NULL")
            connection.send("Ip is null.")
            connection.close()
            sys.exit()

        save_log("INFO", str(cmdstr.split("!")))

        # if cmd for myself
        cname = isMySelfCmd(cmdstr)
        # if batch cmd
        batch = isBatchCmd(cmdstr)
        toPhpData = ""
        if cname:
            try:
                toPhpData = OsaChooseDef(defname=isMySelfCmd(cmdstr), ip=ip, fromPhpCmd=cmdstr)
            except Exception as e:
                save_log("ERROR", e)
        elif batch:
            toPhpData = "BATCH_CMD_OK"
        else:
            toPhpData = hostSocket.proSocket(ip, SOCKET["REMOTE_PORT"], cmdstr)
            save_log("DEBUG", "cmd info:" + str(encode(cmdstr)))
            save_log("DEBUG", "cmd info:" + str(cmdstr))

        if toPhpData == False:
            toPhpData = "Data_receive_Failed!"
        connection.send("%s!%s" % (toPhpData, ip))
        connection.close()
        if toPhpData == "BATCH_CMD_OK":

            try:
                chooseBatchDef(cmdstr, rev)
            except Exception as e:
                save_log("ERROR", "BATCH chooseBatchDef error:" + str(e))
        sys.exit()

    except queueEmptyException, value:
        save_log("ERROR", "server socketDo:" + str(value))
        connection.close()
        sys.exit()