1. 查询本机的设备名及IP地址,打开Python 3.6(32-bit),输入:
import socket
host_name = socket.gethostname()
print(" Host name: %s" %host_name)
print(" IP address: %s" %socket.gethostbyname(host_name))
2. 查询本地的设备名及IP地址,使用内置的类方法,定义成一个独立的函数print_device_info()
import socket
def print_device_info():
host_name = socket.gethostname()
print(" Host name: %s" %host_name)
print(" IP address: %s" %socket.gethostbyname(host_name))
if __name__=='__main__':
print_device_info();
3. 查询远程设备名及IP地址,定义一个函数print_device_remote_info()
import socket
def print_device_remote_info():
host_name = 'home.lenovo'
print(" Host name: %s" %host_name)
print(" IP address: %s" %socket.gethostbyname(host_name))
if __name__=='__main__':
print_device_remote_info();