Search code examples
pythontor

Parse tor exit-address list


Would like to parse IPv4 address from exit-addresses. Format of the file:

ExitNode 006C3FA7C3F6E3ACD13D0DD9B10C7DFA933C237B
Published 2012-06-12 05:04:03
LastStatus 2012-06-12 06:03:22
ExitAddress 220.0.231.71 2012-06-12 10:23:05

Solution

  • with open('data.txt') as inf:
        for line in inf:
            if line.startswith('ExitAddress'):
                print line.split()[1]
    

    will give you

    220.0.231.71
    

    where data.txt contains the four lines you posted (I also ran it with the big file you linked to). Using with will also "automagically" close your file for you when you are done, or an exception is encountered.

    This list comprehension will collect all of the data in a list for you once the data file has been opened:

       a = [line.split()[1] for line in inf if line.startswith('ExitAddress')]
    

    or alternatively, if you don't want to collect the whole list in memory at once, you could use a generator expression:

       g = (line.split()[1] for line in inf if line.startswith('ExitAddress'))
    

    You'll also have take care of closing the file yourself.