#NEWLINE# def is_valid_ip( ip ) : #NEWLINE# #INDENT# def is_valid_ipv4_address( address ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# socket . inet_pton ( socket . AF_INET , address ) #NEWLINE# #UNINDENT# except AttributeError : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# socket . inet_aton ( address ) #NEWLINE# #UNINDENT# except socket . error : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# return ( address . count ( '.' ) == 3 ) #NEWLINE# #UNINDENT# except socket . error : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# return True #NEWLINE# #UNINDENT# def is_valid_ipv6_address( address ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# socket . inet_pton ( socket . AF_INET6 , address ) #NEWLINE# #UNINDENT# except socket . error : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# return True #NEWLINE# #UNINDENT# return ( is_valid_ipv4_address ( ip ) or is_valid_ipv6_address ( ip ) )