Python Forum
completed module: validip.py
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
completed module: validip.py
#5
updated code:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
"""
file          validip.py
purpose       Test given strings to see if they are valid IP addresses.
email         10054452614123394844460370234029112340408691

The intent is that this command works correctly under both Python 2 and
Python 3.  Please report failures or code improvement to the author.
"""

__license__ = """
Copyright (C) 2016, by Phil D. Howard - all other rights reserved

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

The author may be contacted by decoding the number
10054452614123394844460370234029112340408691
(provu igi la numeron al duuma)
"""

from sys import argv, stderr, stdout

from socket import inet_pton, AF_INET, AF_INET6

def validipv4(addr):
    """
function       validipv4
argument       1 (str) that may be an IPv4 address
purpose        Return True if the given IP address is valid for IPv4 or False if not
"""
    try:
        inet_pton(AF_INET,addr)
        r = True
    except (OSError,IOError):
        r = False
    return r


def validipv6(addr):
    """
function       validipv6
argument       1 (str) that maybe an IPv6 address
purpose        Return True if the given IP address is valid for IPv6 or False if not
"""
    try:
        inet_pton(AF_INET6,addr)
        r = True
    except (OSError,IOError):
        r = False
    return r


def validip(addr):
    """
function       validip
argument       1 (str) that maybe an IP address
purpose        Return True if the given IP address is valid for IPv4 or IPv6 or
               False if not either.   
"""
    return validipv4(addr) or validipv6(addr)


def testips(addrlist):
    """
function       testips
argument       List of IP addresses from command line arguments to be tested
purpose        For one IP address, quietly test it for the process exit code.
               for more than one IP address, output a message about each one
               to stdout for capture.
               This is for shell script usage.
"""
    if len(addrlist)==1:
        return 0 if validip(addrlist[0]) else 1
    r = 0
    for addr in addrlist:
        v = 0
        if validipv4(addr):
            v += 4
        if validipv6(addr):
            v += 6
        if v == 0:
            print('IP address {} is NOT valid IPv4 nor valid IPv6'.format(repr(addr)))
        elif v == 4:
            print('IP address {} is valid IPv4'.format(repr(addr)))
        elif v == 6:
            print('IP address {} is valid IPv6'.format(repr(addr)))
        elif v == 10:
            print('IP address {} is valid IPv4 and valid IPv6'.format(repr(addr)))
            r = 1
    return r


def main( args ):
    """
function       main
purpose        Run as a command to test the command line arguments.
"""
    return testips( args[1:] )


if __name__ == '__main__':
    try:
        result = main( argv )
    except KeyboardInterrupt:
        result = 99
        print()
    stdout.flush()
    try:
        exit( int( result ) )
    except ValueError:
        print( str( result ), file=stderr )
        exit( 1 )
    except TypeError:
        if result == None:
            exit( 0 )
        exit( 255 )

# EOF
md5:  762c1c34d361856045811a86da97cadf
sha1: 167d50532205603777d27ed10c190c4770f74544
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
completed module: validip.py - by Skaperen - Dec-27-2016, 10:14 AM
RE: completed module: validip.py - by wavic - Dec-27-2016, 10:22 AM
RE: completed module: validip.py - by Skaperen - Dec-28-2016, 01:58 AM
RE: completed module: validip.py - by Skaperen - Jan-04-2017, 06:39 AM
RE: completed module: validip.py - by micseydel - Dec-27-2016, 11:34 PM
updated code: validip.py - by Skaperen - Dec-28-2016, 05:45 AM
RE: completed module: validip.py - by wavic - Dec-28-2016, 07:02 AM
RE: completed module: validip.py - by Skaperen - Dec-28-2016, 09:53 AM
RE: completed module: validip.py - by micseydel - Jan-02-2017, 05:52 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter Tic Tac Toe With Enhanced Features - Completed adt 2 2,968 Dec-10-2019, 06:22 AM
Last Post: adt
  my earliest completed script Skaperen 0 2,018 Mar-08-2019, 09:50 PM
Last Post: Skaperen
  [link]Creating-a-repo-for-your-completed-scripts metulburr 0 7,647 Aug-29-2018, 01:19 PM
Last Post: metulburr
  Criticism on one of my first completed programs in Python Zombie_Programming 5 4,056 Jul-12-2018, 07:11 AM
Last Post: Zombie_Programming

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020