forked from zhanghe06/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip.py
More file actions
67 lines (54 loc) · 1.45 KB
/
ip.py
File metadata and controls
67 lines (54 loc) · 1.45 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# encoding: utf-8
__author__ = 'zhanghe'
import socket
def get_host_ip():
"""
通过 UDP 获取本机 IP
在 shell 中可以一行调用,获取到本机IP
python -c "import socket;print([(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1])"
:return:
"""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(('8.8.8.8', 80))
ip = s.getsockname()[0]
finally:
s.close()
return ip
def get_local_ip_list():
"""
获取本地ip地址
"""
import os
cmd = "LC_ALL=C ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'"
# result = os.system(cmd)
result = os.popen(cmd).read()
ip_list = result.strip().split('\n')
return ip_list
def check_ip(ipaddr):
"""
校验IP地址正确性
:param ipaddr:
:return:
"""
addr = ipaddr.strip().split('.') # 切割IP地址为一个列表
if len(addr) != 4: # 切割后列表必须有4个参数
return False
for ip in addr:
if ip < 0 or ip > 255:
return False
return True
def check_local_ip(ip):
"""
检测是否为本地ip
:param ip:
:return:
"""
ip_list = get_local_ip_list()
if ip in ip_list:
return True
else:
return False
if __name__ == '__main__':
print(get_local_ip_list())
print(get_host_ip())