1 def get_host_ip(): 2 """ 3 查询本机ip地址 4 :return: ip 5 """ 6 try: 7 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 8 s.connect((‘8.8.8.8‘, 80)) 9 ip = s.getsockname()[0] 10 finally: 11 s.close() 12 return ip
windows linux 下都可使用
原文:https://www.cnblogs.com/LouisZJ/p/9264914.html