1.快速查看主机名和对应的IP地址小程序
1 import socket 2 3 4 def print_machine_info(): # 定义print_machine_info()类 5 host_name = socket.gethostname() # 获取主机名 6 ip_address = socket.gethostbyname(host_name) # 获取主机名对应的IP地址 7 print("Host name: %s" % host_name) # 打印主机名 8 print("IP address: %s" % ip_address) # 打印主机名对应的IP地址 9 10 11 if __name__ == '__main__': 12 print_machine_info() 13 # 在命令行中调用这个模块,print_machine_info()函数会自动运行 14 # 如果从其他脚本中导入,需要用户手动调用这个函数
2.关于gethostname()和gethostbyname()解释
def gethostname(): # real signature unknown; restored from __doc__ """ gethostname() -> string Return the current host name. """ return def gethostbyname(host): # real signature unknown; restored from __doc__ """ gethostbyname(host) -> address Return the IP address (a string of the form '255.255.255.255') for a host. """ pass