>>> #获取hostname... >>> host_name=socket.gethostname()>>> print "%s" %host_nameSimilarFacedeMacBook-Pro.local>>> #获取IP地址... >>> socket.gethostbyname(host_name)'192.168.30.120'
#获取远程机器的IP,类型于pingimport socketdef get_remote_machine_info(remote_host): try: #SimilarFacedeMacBook-Pro:~ similarface$ ping www.python.org #PING python.map.fastly.net (103.245.222.223): 56 data bytes print("%s的IP地址:%s" %(remote_host,socket.gethostbyname(remote_host))) except socket.error,err_msg: print("%s: %s" %(remote_host,err_msg)) #result:
www.python.org的IP地址:103.245.222.223