Ubuntu上的WHOIS命令只返回响应代码



我正试图在Ubuntu上使用python3和whois命令以编程方式查找whois信息。

例如:

import os
# Set domain
hostname = "google.com"
# Query whois
response = os.system("whois " + hostname)
# Check the response
print("This is the Response:" + str(response))

返回此:

。。。MarkMonitor域管理(TM(保护公司和消费者在数字世界中。

访问MarkMonitorhttps://www.markmonitor.com联系我们:+1.8007459229在欧洲,电话+44.020232062220--**这是响应:**0

进程已完成,退出代码为0

whois信息按预期显示(在quote中看不到(,但是,响应始终只是退出代码。

我需要退出代码之前的信息。我必须在whois信息中搜索特定字段。当响应=0时,我该如何处理?

解决方案:

import subprocess
# Set domain
hostname = "google.com"
# Query whois
response = subprocess.check_output(
"whois {hostname}".format(hostname=hostname),
stderr=subprocess.STDOUT,
shell=True)
# Check the response
print(response)

如下所述,应该使用subprocess.check_output((,而不是os.system.

循环时的解决方案:

for domain in domain_list:
hostname = domain
response = subprocess.run(
"whois {hostname}".format(hostname=hostname),
stderr=subprocess.STDOUT,
shell=True)
# Check the response
if response != 0:
available.append(hostname)
else:
continue

Subprocess.run((将继续循环,尽管!=0响应,该响应在域未注册时发生。

尝试使用subprocess.check_output而不是os.system,有关示例,请参阅运行shell命令并捕获输出。

exit_code = os.WEXITSTATUS(response)

return_value = os.popen("whois " + hostname).read()

最新更新