IT虾米网

python获取远程设备的IP地址

wyy 2018年06月24日 编程语言 1166 0
有时需要将主机名转换成对应的IP地址,例如快速查询域名。假如将域名改一个不存在的域名,try-except块会捕捉错误,向用户显示一个错误信息。
    import socket   
    def get_remote_machine_info():   
        remote_host = 'www.python.org'   
        try:   
            print "IP address:%s" %socket.gethostbyname(remote_host)   
        except socket.error,err_msg:   
            print "%s:%s"%(remote_host,err_msg)   
       
       
    if __name__ == "__main__":   
        get_remote_machine_info()  

评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!