Skip to content

Commit deb0a8d

Browse files
committed
新增ip工具方法
1 parent c8c3b2a commit deb0a8d

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

‎tools/ip.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,46 @@
11
# encoding: utf-8
22
__author__ = 'zhanghe'
3+
4+
5+
def get_local_ip_list():
6+
"""
7+
获取本地ip地址
8+
"""
9+
import os
10+
cmd = "LC_ALL=C ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'"
11+
# result = os.system(cmd)
12+
result = os.popen(cmd).read()
13+
ip_list = result.strip().split('\n')
14+
return ip_list
15+
16+
17+
def check_ip(ipaddr):
18+
"""
19+
校验IP地址正确性
20+
:param ipaddr:
21+
:return:
22+
"""
23+
addr = ipaddr.strip().split('.') # 切割IP地址为一个列表
24+
if len(addr) != 4: # 切割后列表必须有4个参数
25+
return False
26+
for ip in addr:
27+
if ip < 0 or ip > 255:
28+
return False
29+
return True
30+
31+
32+
def check_local_ip(ip):
33+
"""
34+
检测是否为本地ip
35+
:param ip:
36+
:return:
37+
"""
38+
ip_list = get_local_ip_list()
39+
if ip in ip_list:
40+
return True
41+
else:
42+
return False
43+
44+
45+
if __name__ == '__main__':
46+
print get_local_ip_list()

0 commit comments

Comments
 (0)