Nagios Livestatus - 套接字python SyntaxError:EOL扫描字符串文字(<string>,第1行)</string>

时间:2013-08-30 15:18:19

标签: python django sockets nagios

我将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 isnt localhost

0 个答案:

没有答案