判断ip是否为内部网络以及ip转int型方法
Ip转int网页工具:
http://www.bejson.com/convert/ip2int/
代码
def check_private_addr(ip):
"""
判断ip是否是内网地址,若返回2的话则为内网ip,若返回1则是外部网络ip
"""
f = unpack('!I', inet_pton(AF_INET, ip))[0] # ip转int类型
print (f)
'''
下面网段可选
'''
private = (
[2130706432, 4278190080], # 127.0.0.0, 255.0.0.0 http://tools.ietf.org/html/rfc3330
[3232235520, 4294901760], # 192.168.0.0, 255.255.0.0 http://tools.ietf.org/html/rfc1918
[2886729728, 4293918720], # 172.16.0.0, 255.240.0.0 http://tools.ietf.org/html/rfc1918
[167772160, 4278190080], # 10.0.0.0, 255.0.0.0 http://tools.ietf.org/html/rfc1918
)
for net in private:
if (f & net[1]) == net[0]:
return 2
return 1