Created
August 17, 2013 03:57
-
-
Save harshavardhana/6255175 to your computer and use it in GitHub Desktop.
Check for valid hostname
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
import re | |
def isvalidhostname(hostname): | |
""" | |
Validate hostname | |
""" | |
regex = re.compile("[^A-Z\d-]", re.IGNORECASE) | |
if len(hostname) > 255: | |
return False | |
# A single trailing dot is legal, strip it if present | |
if hostname.endswith("."): | |
hostname = hostname[:-1] | |
for label in hostname.split("."): | |
test_1 = (label and len(label) <= 63) | |
test_2 = (not label.startswith("-")) | |
test_3 = (not label.endswith("-")) | |
test_4 = (not regex.search(label)) | |
result = (test_1 and test_2 and test_3 and test_4) | |
if not result: | |
return result | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment