-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathipFilter.py
More file actions
33 lines (27 loc) · 1 KB
/
ipFilter.py
File metadata and controls
33 lines (27 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import socket
import struct
class IpFilter():
def __init__(self):
full = self.iptoint("255.255.255.255")
self.mask_list = []
for i in range(0,32):
self.mask_list.append(full >> i << i)
self.control_list = []
f = open('list.txt')
for line in f:
self.control_list.append(line.split())
def iptoint(self,ip):
return socket.ntohl(struct.unpack("I",socket.inet_aton(ip))[0])
def checkrange(self,ip,mask,target):
if (self.iptoint(ip)>>self.mask_list.index(self.iptoint(mask)) == self.iptoint(target)>>self.mask_list.index(self.iptoint(mask))):
return True
else:
return False
def filter(self,target):
for i in range(3,len(self.control_list)):
if self.checkrange(self.control_list[i][0],self.control_list[i][2],target):
return True
return False
if __name__ == '__main__':
f = IpFilter()
print f.filter('220.181.111.86')