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

沒有留言:

張貼留言

Messaging API作為替代方案

  LINE超好用功能要沒了!LINE Notify明年3月底終止服務,有什麼替代方案? LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。 //CHANNEL_ACCESS_TOKEN = 'Messaging ...