How do I open up a text file and use it in this code to sort ip adresses python -
so instead of setting ips sort through have plethora of them in text file need sort though how open , see ones occurred most?
#!/usr/bin/python import iplib ips = [] ip in ["192.168.100.56", "192.168.0.3", "192.0.0.192", "8.0.0.255"]: ips.append(iplib.ipv4address(ip)) def ip_compare(x, y): return cmp(x.get_dec(), y.get_dec()) ips.sort(ip_compare) print [ip.address ip in ips]
and text file looks this
113.177.60.181 - - [05/jul/2013:03:27:07 -0500] "get /email/13948staticvoid.gif http/1.1" 200 17181 "http://www.bereans.org/email/index.php" "mozilla/4.0 (compatible; msie 8.0; windows nt 5.1; trident/4.0)" 113.177.60.181 - - [05/jul/2013:03:27:07 -0500] "get /email/13948staticvoid.gif http/1.1" 200 17181 "http://www.bereans.org/email/index.php" "mozilla/4.0 (compatible; msie 8.0; windows nt 5.1; trident/4.0)"
from collections import counter open('path/to/file') infile: counts = counter(line.split('-', 1)[0].strip() line in infile) ips = counts.most_common() # @jon clements # equivalent ips = sorted(counts, key=counts.__getitem__, reverse=true)
there go! ips
has list of ip addresses in text file, sorted in order frequent least frequent
Comments
Post a Comment