this module is also usable as a command to check if an IP address is valid. given just one IP address, it is silent but sets the exit code to 1 if the address is invalid (0 if valid).
there are functions validip(), validipv4(), and validipv6(). these return boolean values.
there are functions validip(), validipv4(), and validipv6(). these return boolean values.
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from __future__ import absolute_import, division, print_function, unicode_literals """ file validip.py purpose Check if IP addresses (in strings) are valid IPv4 or IPv6 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 """ from sys import argv, stderr, stdout, version from socket import inet_pton, AF_INET, AF_INET6 def validipv4(addr): """validipv4""" try: inet_pton(AF_INET,addr) r = True except (OSError,IOError): r = False return r def validipv6(addr): """validipv6""" try: inet_pton(AF_INET6,addr) r = True except (OSError,IOError): r = False return r def validip(addr): if validipv4(addr): return True if validipv6(addr): return True return False def testips(addrlist): if len(addrlist)==1: return 0 if validip(addrlist[0]) else 1 r = 0 for addr in addrlist: e = 0 if validipv4(addr): print('IP address {} is valid IPv4'.format(repr(addr))) else: print('IP address {} is NOT valid IPv4'.format(repr(addr))) e += 4 if validipv6(addr): print('IP address {} is valid IPv6'.format(repr(addr))) else: print('IP address {} is NOT valid IPv6'.format(repr(addr))) e += 6 if e == 10: r = 1 return r def main( args ): """main""" return testips( args[1:] ) if __name__ == '__main__': try: result = main( argv ) except KeyboardInterrupt: result = 99 print( '' ) except IOError: result = 98 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
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.