Ubuntu 上的 WHOIS 命令只有 returns 个响应代码?

WHOIS command on Ubuntu only returns a response code?

我正在尝试使用 python3 和 whois command on Ubuntu.

以编程方式查找 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))

Returns这个:

...MarkMonitor Domain Management(TM) Protecting companies and consumers in a digital world.

Visit MarkMonitor at https://www.markmonitor.com Contact us at +1.8007459229 In Europe, at +44.02032062220 -- **This is the Response:**0

Process finished with exit code 0

whois 信息按预期显示(未在引用中显示),但是,响应始终只是退出代码。

我需要退出代码之前的信息。我必须搜索特定字段的 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,请参阅 Running shell command and capturing the output 示例。

exit_code = os.WEXITSTATUS(response)

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