简体   繁体   中英

how to get unique ip address from list of ip address present in log file using python?

I have a log file in text file format. the log file looks like the below format

220.227.40.118 - - [06/Mar/2012:00:00:00 -0800] "GET /mysidebars/newtab.html 
HTTP/1.1" 404 0 - -
220.227.40.118 - - [06/Mar/2012:00:00:00 -0800] "GET /hrefadd.xml HTTP/1.1" 
204 214 - -
59.95.13.217 - - [06/Mar/2012:00:00:00 -0800] "GET /dbupdates2.xml HTTP/1.1" 
404 0 - -

111.92.9.222 - - [06/Mar/2012:00:00:00 -0800] "GET /mysidebars/newtab.html 
HTTP/1.1" 404 0 - -
120.56.236.46 - - [06/Mar/2012:00:00:00 -0800] "GET /hrefadd.xml HTTP/1.1" 
204 214 - -
49.138.106.21 - - [06/Mar/2012:00:00:00 -0800] "GET /add.txt HTTP/1.1" 204 
214 - -

117.195.185.130 - - [06/Mar/2012:00:00:00 -0800] "GET 
/mysidebars/newtab.html HTTP/1.1" 404 0 - -
122.160.166.220 - - [06/Mar/2012:00:00:00 -0800] "GET 
/mysidebars/newtab.html HTTP/1.1" 404 0 - -
117.214.20.28 - - [06/Mar/2012:00:00:00 -0800] "GET /welcome.html HTTP/1.1" 
204 212 - -
117.18.231.5 - - [06/Mar/2012:00:00:00 -0800] "GET /mysidebars/newtab.html 
HTTP/1.1" 404 0 - -

I want to find each unique ip address present in the log file using python.

How about:

def get_ips(logfile):
    with open(logfile, 'r') as f:
        for line in f.readlines():
            yield line.split()[0]


def main():
    for ip in set(get_ips('log.txt')):
        print ip


if __name__ == '__main__':
    main()

Here is how :

def unique_ips():
    f = open('log_file.txt','r')
    ips = set()
    for line in f:
        ip = line.split()[0]
        ips.add(ip)
    return ips

if __name__=='__main__':
    print unique_ips()

This should work fine with python 2.6 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM