def get_machine_name(): return socket.gethostname() machine_name = get_machine_name() print(f"Machine Name: {machine_name}") 在这个示例中,我们首先导入了socket模块,然后定义了一个函数get_machine_name(),该函数返回socket.gethostname()的值,即当前机器的名称。最后,通过调用该函数并打印结果,我们...
we imported the socket module in the first line and then used the function gethostname() to get the Hostname of the device. This function will return the device hostname where the python code is interpreted.
import socket def print_machine_info(): host_name=socket.gethostname() ip_address=socket.gethostbyname(host_name) print"Host name: %s"%host_name print"IP address: %s"%ip_addressif__name__ =='__main__': print_machine_info() 输出 Host name: ubuntu IP address:127.0.1.1...
importsocket defprint_machine_info(): host_name=socket.gethostname() ip_address=socket.gethostbyname(host_name) print"Host name:%s"%host_name print"IP address:%s"%ip_address if__name__=='__main__': print_machine_info() Test: Host name:kevins-MacBook-Pro.local IP address:192.168.0.10...
host_name=socket.gethostname() ip_address=socket.gethostbyname(host_name) print"Host name: %s"%host_name print"IP address: %s"%ip_addressif__name__ =='__main__': print_machine_info() 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. ...
("硬盘型号: %s"%disk.Model)print("磁盘大小: %.2fGB"%(diskSize/1024**3))#获取显卡信息forxkinw.Win32_VideoController():print("显卡名称: %s"%xk.name)print("")#获取计算机名称和IPhostname=socket.gethostname()ip=socket.gethostbyname(hostname)print("计算机名称: %s"%hostname)print("IP地址...
("磁盘大小: %.2fGB" %(diskSize/1024**3)) #获取显卡信息 for xk in w.Win32_VideoController(): print("显卡名称: %s" %xk.name) print("") #获取计算机名称和IP hostname = socket.gethostname() ip = socket.gethostbyname(hostname) print("计算机名称: %s" %hostname) print("IP地址: %s"...
笔者有时候需要根据hostname获取ip 比如根据machinepany获得ip10.173.14.117 本文地址http://blog.csdn.net/never_cxb/article/details/47831 方法1:利用socket模块里的gethostbyname函数 代码如下,使用socket模块 importsocket socket.gethostbyname(“.baidu”) ‘61.135.169.125’ socket.gethostbyname(“rs.xidian.edu”)...
hostname = socket.gethostname() ip = socket.gethostbyname(hostname) print("计算机名称: %s" %hostname) print("IP地址: %s" %ip) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21.
serversocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)#getlocal machine name host=socket.gethostname()port=8088# bind to the portserversocket.bind((host,port))print("Server start at port: 8088")# queue up to5requests serversocket.listen(5)whileTrue:# establish a connection ...