Skip to content

Instantly share code, notes, and snippets.

@paomian
Created August 25, 2014 09:53
Show Gist options
  • Select an option

  • Save paomian/435bb44fc1fc40d16afa to your computer and use it in GitHub Desktop.

Select an option

Save paomian/435bb44fc1fc40d16afa to your computer and use it in GitHub Desktop.
#! /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()
@defp
Copy link

defp commented Aug 25, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment