获取本机HOST name和局域网IP
import socketdef print_machine_info(): host_name = socket.gethostname() ip_address = socket.gethostbyname(host_name) print("Host name is:%s" % host_name) print("IP address is:%s" % ip_address)if __name__ == '__main__': print_machine_info()
输出
Host name is:jomax-mbp.lanIP address is:192.168.1.15
获取远程设备的IP地址
# -*- coding: UTF-8 -*-import socketdef get_remote_machine_info(err_msg=None): remote_host = 'www.baidu.com' try: print("主机的IP地址是:%s" % socket.gethostbyname(remote_host)) except (socket.error, err_msg): print("%s: %s" % (remote_host, err_msg))if __name__ == '