Perl Hacker, Python Initiate

Hrvoje Niksic hniksic at
Thu Feb 3 10:41:22 EST 2011

Gary Chambers <gwchamb at> 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", "", "", "axfr"],

# 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 ='regexp1', line)
    if m:
        cnames[].append(  # push ...
    m ='regexp2', line)
    if m:
        ip[] =

More information about the Python-list mailing list