Last active
December 11, 2015 20:58
-
-
Save zhovner/4658838 to your computer and use it in GitHub Desktop.
Python network
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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