2018年1月11日 星期四

Python: Valid a IP address

Python: Valid a IP address

Python Basic: Exercise-139 with Solution

Write a Python program to valid a IP address.
Sample Solution:-
import socket

def is_valid_ipv4_address(address):
    try:
        socket.inet_pton(socket.AF_INET, address)
    except AttributeError:  # no inet_pton here, sorry
        try:
            socket.inet_aton(address)
        except socket.error:
            return False
        return address.count('.') == 3
    except socket.error:  # not a valid address
        return False

    return True

address='127.0.0.1'

if is_valid_ipv4_address(address):
   print("Valid IP")


addr = '127.0.0.2561'
try:
    socket.inet_aton(addr)
    
except socket.error:
    print("Invalid IP")

========== RESTART: F:/Python_APSC/py-ex-basic-139.py =========
Valid IP
Invalid IP
>>> 

沒有留言:

張貼留言

113 學年度第 1 學期 RFID應用課程 Arduino程式

113 學年度第 1 學期 RFID應用課程 Arduino程式 https://www.mediafire.com/file/zr0h0p3iosq12jw/MFRC522+(2).7z/file 內含修改過後的 MFRC522 程式庫 (原程式有錯誤) //定義MFRC522...