FAQ
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.

Thanks.

Search Discussions

  • Harrismh777 at Jun 7, 2011 at 1:55 am

    Eric wrote:
    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.
    You might try the os module and then use something like nslookup.


    import os
    os.system('nslookup <name>')


    The output is sent on the subprocess standard out... so you can grab it
    with piping, or redirect, or redirect to a file and read later, etc.

    You might also try the subprocess module. It provides better flexibility
    and control for handling the output of the nslookup, or whatever tool
    you decide to use to find the fully qualified name.

    kind regards,
    m harris
  • Chris Angelico at Jun 7, 2011 at 2:50 am

    On Tue, Jun 7, 2011 at 10:40 AM, Eric 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
  • Philip Semanchuk at Jun 7, 2011 at 3:10 am

    On Jun 6, 2011, at 8:40 PM, Eric 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.
    The ones here served me very well:
    http://pyxml.cvs.sourceforge.net/viewvc/pyxml/xml/xml/Uri.py?revision=1.1&view=markup

    bye
    Philip
  • Nobody at Jun 7, 2011 at 5:23 am

    On Mon, 06 Jun 2011 17:40:29 -0700, Eric wrote:

    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.
    Try socket.getfqdn() or socket.gethostbyname_ex().

    With one exception[1], you can't reliably do it just by examining the
    string; you have to ask the resolver.

    [1] If a hostname ends with a dot, it's fully qualified.
  • Chris Angelico at Jun 7, 2011 at 5:52 am

    On Tue, Jun 7, 2011 at 3:23 PM, Nobody wrote:
    [1] If a hostname ends with a dot, it's fully qualified.
    Outside of BIND files, when do you ever see a name that actually ends
    with a dot?

    ChrisA
  • Chris Torek at Jun 7, 2011 at 6:20 am

    On Tue, Jun 7, 2011 at 3:23 PM, Nobody wrote:
    [1] If a hostname ends with a dot, it's fully qualified.
    [otherwise not, so you have to use the resolver]

    In article <mailman.2521.1307425928.9059.python-list at python.org>,
    Chris Angelico wrote:
    Outside of BIND files, when do you ever see a name that actually ends
    with a dot?
    I type them in this way sometimes, when poking at network issues. :-)
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40?39.22'N, 111?50.29'W) +1 801 277 2603
    email: gmail (figure it out) http://web.torek.net/torek/index.html
  • Nobody at Jun 7, 2011 at 8:46 pm

    On Tue, 07 Jun 2011 15:52:05 +1000, Chris Angelico wrote:

    [1] If a hostname ends with a dot, it's fully qualified.
    Outside of BIND files, when do you ever see a name that actually ends
    with a dot?
    Whenever it is entered that way.

    This may be necessary on complex networks with local subdomains, i.e.
    where resolv.conf has "options ndots:2". E.g. "foo.it" might resolve to
    "foo.it.bar.edu" (in bar.edu's IT department's subdomain), requiring a
    trailing dot if you want the Italian site "foo.it".

    The canonical real-world example of this used to be foo.cs resolving to
    foo.cs.berkeley.edu (UCB Comp. Sci. department), but ever since .cs split
    into .cz and .sk it's no longer ambiguous.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJun 7, '11 at 12:40a
activeJun 7, '11 at 8:46p
posts8
users6
websitepython.org

People

Translate

site design / logo © 2022 Grokbase