0

我将 LQL 查询发送到 Livestatus 并通过套接字发送。

当我在调试时,它可以工作!但是当我在没有调试的情况下运行时,我收到了这个错误:

SyntaxError: EOL while scanning string literal (<string>, line 1)

我的代码是:

    len(__nagios_query(('GET services\nFilter: display_name ~~ test\nFilter: state = 0\n')))

def __nagios_query(query):
    ''' Receives the LQL query to Livestatus and sends through a socket.
    Parse the return into lists and dictionaries and return ready to use. '''

    result_list = []
    query += "OutputFormat: python\n"

    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((NG_HOST, NG_PORT))
        s.sendall(query)
        s.shutdown(socket.SHUT_WR)

        answer = s.recv(NG_TIMEOUT)

        s.close()


        result = eval(answer)
        columns = result.pop(0)

        for item in result:
            item = dict(zip(columns, item))
            result_list.append(item)

    except Exception, e:
        raise e

    return result_list

有了这条新线,它可以工作,但不优雅,还有另一种方式吗?

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((NG_HOST, NG_PORT))
    s.sendall(query)
    s.shutdown(socket.SHUT_WR)

    #New line
    time.sleep(0.1)

    answer = s.recv(NG_TIMEOUT)

ps: NG_HOST 不是本地主机

4

0 回答 0