以文字“只是另一个初学者”作为开头。当您通过 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)
请您参考如下方法:
要回答您的直接问题,请在 whois 命令的输出中查找此字符串以查看是否存在问题...
No match for "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)
