2

Prefacing this with the text 'just another beginner'. when you have the result of a whois command via the Popen command, how do you test if its good ?

Normally when Python returns a list of whatever you can test the length of it and that has usually sufficed for me, but this is a little more arbitrary.

eg im testing for a domains country of origin, but sometimes the domains that gethostbyaddr gives me are not recognised by the WHOIS server. So, i thought i would go with sending it an ip in case of failure but I've ended up with this not so pretty less than 70 characters test. Just wondering if anyone knows what the 'standard' way of doing this is.

w = Popen(['whois', domain], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
                whois_result = w.communicate()[0]
                print len(whois_result)
                if len(whois_result) <= 70:
                        w = Popen(['whois', p_ip], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
                        whois_result = w.communicate()[0]
                        print len(whois_result)
                        if len(whois_result) <= 70:
                                print "complete and utter whois failure, its you isnt it, not me."
                        test = re.search("country.+([A-Z].)",whois_result)
                        countryid = test.group(1)
Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
  • Is the information you're looking for available in DNS? Maybe pydns or dnspython could work? – Brian Cain Nov 27 '11 at 23:02
  • its the country of origin, I dont believe DNS stores that information. correct me if Im wrong. Ive tried pywhois with this and it wouldnt take IP addresses so I fell back to OS stuff. –  Nov 27 '11 at 23:05
  • Do not use Popen to launch a whois command, use the appropriate whois libraries inside of Python, or just open a TCP socket on port 43 and read RFC3912 for all details. – Patrick Mevzek Jan 04 '18 at 15:17

1 Answers1

1

To answer your direct question, look for this string in the output of a whois command to see whether there was a problem...

No match for "insert_domain_here"

To address other meaningful issues to your task... your Popen command is going at things the hard way... you don't need a PIPE for stdin and you can call .communicate() directly on the Popen to make this a bit more efficient... I rewrote with what I think you have in mind...

from subprocess import Popen, PIPE, STDOUT
import re

## Text result of the whois is stored in whois_result...
whois_result = Popen(['whois', domain], stdout=PIPE,
    stderr=STDOUT).communicate()[0]
if 'No match for' in whois_result:
    print "Processing whois failure on '%s'" % domain
    whois_result = Popen(['whois', p_ip], stdout=PIPE,
        stderr=STDOUT).communicate()[0]
    if 'No match for' in whois_result:
            print "complete and utter whois failure, its you isnt it, not me."
    test = re.search("country.+([A-Z].)",whois_result)
    countryid = test.group(1)
Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
  • oh really! (first off many thanks!) you can just check the result in an if statement ? wow! thankyou for cleaning up those two commands, this is the first time ive encountered this Popen thing and it took me a while to get it to return anything, much appreciated. cups of tea all round. –  Nov 27 '11 at 23:25
  • 1
    perfect - but my whois returns 'Not found : [something]' but yeh - awesome work, thankyou. no relation to james from detroit ? –  Nov 27 '11 at 23:31
  • @cigar, you are most welcome. AFAIK, I don't have relatives in Detroit – Mike Pennington Nov 27 '11 at 23:35
  • hes a musician, rough detroit techno style. thanks again, working perfectly, and much more elegant (apart from the obv clunk of the OS command itself :) –  Nov 27 '11 at 23:37
  • am i allowed to ask how to suppress the output on this same thread ? :) oops never mind found this: http://stackoverflow.com/questions/699325/suppress-output-in-python-calls-to-executables –  Nov 27 '11 at 23:51