Home | Trees | Indices | Help |
---|
|
1 # Copyright (c) 2017 Hubert Kario 2 # 3 # See the LICENSE file for legal information regarding use of this file. 4 5 """Utilities for handling DNS hostnames""" 6 7 import re 8 911 """ 12 Check if the parameter is a valid hostname. 13 14 @type hostname: str or bytearray 15 @rtype: boolean 16 """ 17 try: 18 if not isinstance(hostname, str): 19 hostname = hostname.decode('ascii', 'strict') 20 except UnicodeDecodeError: 21 return False 22 if hostname[-1] == ".": 23 # strip exactly one dot from the right, if present 24 hostname = hostname[:-1] 25 # the maximum length of the domain name is 255 bytes, but because they 26 # are encoded as labels (which is a length byte and an up to 63 character 27 # ascii string), you change the dots to the length bytes, but the 28 # host element of the FQDN doesn't start with a dot and the name doesn't 29 # end with a dot (specification of a root label), we need to subtract 2 30 # bytes from the 255 byte maximum when looking at dot-deliminated FQDN 31 # with the trailing dot removed 32 # see RFC 1035 33 if len(hostname) > 253: 34 return False 35 36 # must not be all-numeric, so that it can't be confused with an ip-address 37 if re.match(r"[\d.]+$", hostname): 38 return False 39 40 allowed = re.compile(r"(?!-)[A-Z\d-]{1,63}(?<!-)$", re.IGNORECASE) 41 return all(allowed.match(x) for x in hostname.split("."))42
Home | Trees | Indices | Help |
---|
Generated by Epydoc 3.0.1 | http://epydoc.sourceforge.net |