Skip to content

Instantly share code, notes, and snippets.

@harshavardhana
Created August 17, 2013 03:57
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 harshavardhana/6255175 to your computer and use it in GitHub Desktop.
Save harshavardhana/6255175 to your computer and use it in GitHub Desktop.
Check for valid hostname
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