import socket def get_local_ip(): """ 获取本机 IP 地址: 通用 """ try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(('8.8.8.8', 80)) ip = s.getsockname()[0] finally: s.close() return ip
from django.core.management.commands.runserver import Command as runserver import socket def get_local_ip(): """ 获取本机 IP 地址: 只能捕获通过runserver启动的Django服务 """ r = runserver() ip = socket.gethostbyname(socket.gethostname()) return ip
标签:socket,ip,runserver,获取,本地,import,Django From: https://www.cnblogs.com/52-qq/p/17286359.html