Created
April 10, 2012 07:48
-
-
Save TkTech/2349131 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> % python silly_ip.py | |
There are 1 items in the 74 bucket. | |
(74, 125, 232, 201) 1 | |
(74, 125, 232, 211) 1 | |
(74, 125, 232, 206) 1 | |
There are 1 items in the 79 bucket. | |
(79, 115, 74, 141) 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf8 -*- | |
import sys | |
dummy_data = [ | |
'74.125.232.201', | |
'74.125.232.206', | |
'74.125.232.211', | |
'79.115.74.141' | |
] | |
def main(argv): | |
root = {} | |
for raw_ip in dummy_data: | |
ip = [int(i) for i in raw_ip.split('.')] | |
tmp = root.setdefault(ip[0], {}) | |
tmp = tmp.setdefault(ip[1], {}) | |
tmp = tmp.setdefault(ip[2], {}) | |
tmp[ip[3]] = tmp.get(ip[3], 0) + 1 | |
# Found how many there are on each level. | |
for ip1, v1 in root.items(): | |
print('There are %d items in the %d bucket.' % (len(v1), ip1)) | |
for ip2, v2 in v1.items(): | |
for ip3, v3 in v2.items(): | |
for ip4, count in v3.items(): | |
print (ip1, ip2, ip3, ip4), count | |
return 0 | |
if __name__ == '__main__': | |
sys.exit(main(sys.argv)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment