Created
August 25, 2014 09:53
-
-
Save paomian/435bb44fc1fc40d16afa to your computer and use it in GitHub Desktop.
This file contains hidden or 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/python | |
| # -*- coding: utf-8 -*- | |
| import struct | |
| import socket | |
| def ip2int(ip): | |
| '''将ip映射到int范围,并保证从0.0.0.0到255.255.255.255递增''' | |
| return struct.unpack("!I", socket.inet_aton(ip))[0] | |
| def find(num, l): | |
| n = len(l) | |
| while num < l[n - 1] or num > l[n]: | |
| if num > l[n / 2]: | |
| n += n / 2 | |
| else: | |
| n /= 2 | |
| return n | |
| def check_ip(ip): | |
| """docstring for check_ipfname""" | |
| # f = open("test.txt") | |
| print ip | |
| print ip | |
| ii = ip2int(ip) | |
| f = open("mi_china.txt","r") | |
| line = f.readline() | |
| l = [] | |
| dic = {} | |
| while line: | |
| w = line.split(',') | |
| l.append(long(w[0])) | |
| dic[long(w[0])] = long(w[1]) | |
| # print int(w[0]), int(w[1]) | |
| line = f.readline() | |
| f.close() | |
| tmp = l[find(ii, l)] | |
| print tmp, dic[tmp] | |
| if tmp <= ii <= dic[tmp]: | |
| return True | |
| else: | |
| return False | |
| if __name__ == '__main__': | |
| f = open("mi_china.txt","r") | |
| line = f.readline() | |
| i = 0 | |
| while line: | |
| w = line.split(',')[2] | |
| if check_ip(w) and w == "1.0.1.0": | |
| i += 1 | |
| print "hhh" | |
| print i | |
| f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/paomian/a1668e8b1d60a52e60b1