Perl Hacker, Python Initiate

Hrvoje Niksic hniksic at xemacs.org
Thu Feb 3 16:41:22 CET 2011


Gary Chambers <gwchamb at gwcmail.com> writes:

> Will someone please provide some insight on how to accomplish that
> task in Python?  I am unable to continually (i.e. it stops after
> displaying a single line) loop through the output while testing for
> the matches on the two regular expressions.  Thank you.

If I understand you correctly, here is the relevant part (untested):

import subprocess, collections

dig = subprocess.Popen(["dig", "ns.example.com", "example.com", "axfr"],
                       stdout=subprocess.PIPE).stdout

# defaultdict allows the equivalent of push @{$x{$y}}, $z
cnames = collections.defaultdict(list)
ip = {}

for line in dig:
    if line.startswith(';'):
        continue # Skip any comments
    m = re.search(r'regexp1', line)
    if m:
        cnames[m.group(2)].append(m.group(1))  # push ...
    m = re.search(r'regexp2', line)
    if m:
        ip[m.group(1)] = m.group(2)



More information about the Python-list mailing list