且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-08-26 15:50:11

这里使用了三个知识点:

1、函数的定义

2、用户自定义输入input

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

1
2
3
4
5
6
7
8
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 使用socket通过用户自定义输入获得远程设备IP


注意事项:

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




本文转自 yuri_cto 51CTO博客,原文链接:http://blog.51cto.com/laobaiv1/1956322,如需转载请自行联系原作者