Validating string for FDQN

Chris Angelico rosuav at gmail.com
Mon Jun 6 22:50:22 EDT 2011


On Tue, Jun 7, 2011 at 10:40 AM, Eric <eric.wong.t at gmail.com> wrote:
> Hello,
>
> Is there a library or regex that can determine if a string is a fqdn
> (fully qualified domain name)? I'm writing a script that needs to add
> a defined domain to the end of a hostname if it isn't already a fqdn
> and doesn't contain the defined domain.

One reliable way to test would be to do a whois check on the name. If
it comes up with something, it's fully qualified.

http://code.google.com/p/pywhois/

Alternatively, if all you want is a simple syntactic check, and if you
can assume that the name is already a valid domain name (no weird
characters, etc), then you can simply divide it on the last dot and
see if the last part is a recognized TLD. A partial list of TLDs can
be found here:

http://data.iana.org/TLD/tlds-alpha-by-domain.txt

There are other TLDs too, including .localhost and .test, which you
can probably ignore.

Chris Angelico



More information about the Python-list mailing list