Python Popen WHOIS 操作系统命令失败测试
在此开头加上“只是另一个初学者”的文字。当您通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要回答您的直接问题,请在
whois
命令的输出中查找此字符串,以查看是否存在问题...要解决您的任务中的其他有意义的问题...您的
Popen
命令正在以困难的方式处理事情...您不需要PIPE
对于stdin
,您可以直接在Popen
上调用.communicate()
来提高效率......我用什么重写了我想你心里有...To answer your direct question, look for this string in the output of a
whois
command to see whether there was a problem...To address other meaningful issues to your task... your
Popen
command is going at things the hard way... you don't need aPIPE
forstdin
and you can call.communicate()
directly on thePopen
to make this a bit more efficient... I rewrote with what I think you have in mind...