python剑指网络

  

>>> #获取hostname
... 
>>> host_name=socket.gethostname()
>>> print "%s" %host_name
SimilarFacedeMacBook-Pro.local
>>> #获取IP地址
... 
>>> socket.gethostbyname(host_name)
'192.168.30.120'
#获取远程机器的IP,类型于ping
import socket
def 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

  

  

posted @ 2016-05-23 18:16  similarface  阅读(159)  评论(0编辑  收藏  举报