Skip to content

Instantly share code, notes, and snippets.

@zhovner zhovner/netknife.py
Last active Dec 11, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.