这里使用了三个知识点:

1、函数的定义

2、用户自定义输入input

3、try-except模块捕获了错误,如果失败向用户显示了一个自定义的错误消息

def get_remote_sysinfo():
    print("Example:www.baidu.com")
    remote_host = input("Please input remot_host:")
    try:
        print("IP address: %s" %socket.gethostbyname(remote_host))
    except socket.error:
        print ("Sorry!~ Don't Found %s" %remote_host)
get_remote_sysinfo()


执行正确结果:

python 使用socket通过用户自定义输入获得远程设备IP_python


错误结果:

python 使用socket通过用户自定义输入获得远程设备IP_使用_02


注意事项:

函数定义:函数内代码要缩进,不然会语法报错,切记!~