Python Popen WHOIS 操作系统命令失败测试

发布于 2024-12-18 10:18:01 字数 963 浏览 2 评论 0原文

在此开头加上“只是另一个初学者”的文字。当您通过 Popen 命令获得 whois 命令的结果时,如何测试其是否良好?

通常,当Python返回一个列表时,你可以测试它的长度,这通常对我来说已经足够了,但这有点随意。

例如 我正在测试域名的原籍国,但有时 WHOIS 服务器无法识别 gethostbyaddr 给我的域名。所以,我想我会发送一个 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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦中的蝴蝶 2024-12-25 10:18:01

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

与“insert_domain_here”不匹配

要解决您的任务中的其他有意义的问题...您的 Popen 命令正在以困难的方式处理事情...您不需要 PIPE 对于 stdin ,您可以直接在 Popen 上调用 .communicate() 来提高效率......我用什么重写了我想你心里有...

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文