python 使用socket通過用戶自定義輸入獲得遠程設備IP

這裏使用了三個知識點:

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()


執行正確結果:

wKioL1mSXGyiOVA2AAA36oLFvv8467.jpg-wh_50


錯誤結果:

wKioL1mSXJPRcbchAAA1bGUMhps465.jpg-wh_50


注意事項:

函數定義:函數內代碼要縮進,不然會語法報錯,切記!~



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章