# 获取系统ip
import socket
2
3def get_local_ip():
4 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
5 try:
6 # 连接到一个公共DNS服务器,例如Google DNS
7 s.connect(('8.8.8.8', 80))
8 # 返回连接时分配给我们的本地IP地址
9 return s.getsockname()[0]
10 finally:
11 s.close()
12
13local_ip = get_local_ip()
14print(local_ip)