Python Popen WHOIS OS命令失败测试

时间:2011-11-27 22:56:13

标签: python networking whois

将其与“只是另一个初学者”的文字作为前缀。当你通过Popen命令得到一个whois命令的结果时,你如何测试它的好处?

通常情况下,当Python返回一个列表,你可以测试它的长度,这对我来说通常是足够的,但这更加随意。

例如 我正在测试域名来源国,但有时gethostbyaddr给我的域名不会被WHOIS服务器识别。所以,我想我会在发生故障的情况下向它发送一个ip,但我最终得到的不是那么不到70个字符的测试。只是想知道是否有人知道这样做的“标准”方式是什么。

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)

1 个答案:

答案 0 :(得分:1)

要回答您的直接问题,请在whois命令的输出中查找此字符串,以查看是否存在问题...

  

“insert_domain_here”不匹配

要解决您的任务中的其他有意义的问题...您的Popen命令正在艰难地处理...您不需要PIPE stdin而你可以直接在.communicate()上调用Popen来提高效率......我改写了我认为你的想法......

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)