3

I have to write python code to:

  1. Read text file as input (separated by tabs and usually two columns)
  2. Check the httpd.conf file for that parameter and its value

For example, I have a text file like this:

KeepAlive  on
Listen     80
TCP        On

and a normal httpd.conf file too.

I want to check and compare each line fields and if the config was correct, then print keepalive is ok for example.

I wrote this:

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
JohnE
  • 29,156
  • 8
  • 79
  • 109
user179902
  • 31
  • 4
  • Are you assuming that the contents of "httpd.conf" are correct and want to verify that the data in "config.txt" matches the data in "httpd.conf"? Or is it the other way around, and "config.txt" is correct and you're trying to validate "httpd.conf"? – PM 2Ring May 24 '15 at 13:36
  • Also, you say that most of the time "config.txt" has 2 tab-separated columns. Do some lines have 3 or more columns? If so, what should your program do with those extra fields? – PM 2Ring May 24 '15 at 13:49
  • There are some useful answers here: * http://stackoverflow.com/questions/237209/any-python-libs-for-parsing-apache-config-files First answer links to a class that converts Apache configuration into Python data structure and allows querying it. – Anton Strogonoff May 24 '15 at 15:22
  • yes , Config.txt is correct and httpd.conf must be like that.×Comments may only be edited for 5 minutes×Comments may only be edited for 5 minutes×Comments may only be edited for 5 minutes – user179902 May 25 '15 at 05:43
  • Thanks for your replies .. – user179902 May 30 '15 at 11:17

1 Answers1

0

Finaly i wrote something that works (Below), but there is one thing is remaining (match exact words AND ignoring cases).any help

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())
user179902
  • 31
  • 4