Python Code To Determine If Tld Exists, Prompt Again If Not?
This is an additional question in regards to this post: Python raw_input with forced TLD? I have a check in place to see if a string ends with a TLD, TLD = ('.com', '.info', '.org
Solution 1:
Something like this:
TLD = ('.com', '.info', '.org', '.net')
hostName = raw_input(":").lower()
whilenot hostName.endswith(TLD):
print"Incorrect input, Try again!"
hostName = raw_input(":").lower()
Demo:
:foo.bar
Incorrect input, Try again!
:google.in
Incorrect input, Try again!
:yahoo.com
Solution 2:
Actual DNS lookup to test a TLD
Ohh, while we are at it, maybe a short snippet to actually test TLD's against the DNS servers might become handy. I'm using the dnspython
module from the Nominum guys:
import dns.resolver
deftestTLD(tld):
try:
dns.resolver.query(tld + '.', 'SOA')
returnTrueexcept dns.resolver.NXDOMAIN:
returnFalsefor tld in ('com', 'org', 'klonk', 'dk'):
print"TLD \"{tld}\" exists: {bool}".format(tld=tld, bool=testTLD(tld))
and it runs like this:
TLD "com" exists: True
TLD "org" exists: True
TLD "klonk" exists: False
TLD "dk" exists: True
Post a Comment for "Python Code To Determine If Tld Exists, Prompt Again If Not?"