Skip to content

Instantly share code, notes, and snippets.

@zhovner
Last active December 11, 2015 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhovner/4658838 to your computer and use it in GitHub Desktop.
Save zhovner/4658838 to your computer and use it in GitHub Desktop.
Python network
# Validate IP address
import socket
def valid_ip(ipaddress):
try:
socket.inet_aton(ipaddress)
return True
except:
return False
# Validate domain name
def valid_domain(domainstring):
# Lookahead makes sure that it has a minimum of 4 (a.in) and a maximum of 255 characters
# One or more labels (separated by periods) of length between 1 to 63, starting and ending with alphanumeric characters, and containing alphanumeric chars and hyphens in the middle.
# Followed by a top level domain name (whose max length is 5 for museum)
dpattern = '^(?=.{4,255}$)([a-zA-Z0-9]{,61}[a-zA-Z0-9]\.)+[a-zA-Z]{2,5}$'
if re.match(dpattern, domainstring):
return True
else:
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment