如何在httpd.conf中检查配置参数

时间:2015-05-24 12:10:26

标签: python string python-3.x

我必须编写python代码:

  1. 将文本文件作为输入读取(由制表符分隔,通常为两列)
  2. 检查httpd.conf文件中的参数及其值
  3. 例如,我有一个这样的文本文件:

    KeepAlive  on
    Listen     80
    TCP        On
    

    以及正常的httpd.conf文件。

    我想检查并比较每个行字段,如果配置正确,则打印keepalive is ok例如。

    我写了这个:

    d = []
    with open("config.txt") as CFGF:
        for line in CFGF:
            key, val = line.split()
            c = key, val
            d.extend(c)
    
    with open("httpd.conf") as f:
        j = 0
        for i in d:
            for line in f:
                ls = line.strip()
                if d[j] in line:
                    if d[j + 1] in line:
                        print(line.rsplit())
            j += 1
    

1 个答案:

答案 0 :(得分:0)

最后我写了一些有用的东西(下图),但还有一件事情就是存在(匹配确切的单词和忽略的情况)。任何帮助

l = []
with open('config.txt') as cfg:
    for line in cfg:
        l.extend(line.split())
a, b = zip(*(s.split("~") for s in l))
for w in range(len(a)):
    with open('httpd.conf') as apache:
        for lines in apache:
            if (a[w]) in lines and not lines.startswith("#") and not lines.__contains__("#"):
                if (b[w]) in lines:
                    print(lines.rstrip())