python - 如何在 python 中正确执行 host 或 dig 命令

标签 python django ubuntu network-programming blacklist

我想使用 python 处理主机或挖掘命令来检查域是否被列入黑名单。我用这些

surbl_result = os.system(host_str + ".multi.surbl.org")
#this works like performing a terminal command which is host johnnydeppsource.com.multi.surbl.org

它返回一个整数 0(表示它在黑名单中列出)或 256(它未列出)的响应
if surbl_result == 0: #blacklisted in surbl
    black_list = True

但有时,主机命令失败并给出服务失败响应
Host johnnydeppsource.com.multi.surbl.org not found: 2(SERVFAIL)

这将返回一个零值,允许它添加新域,即使它被列入黑名单。还有其他方法可以执行这种事情吗?这包含在我的 django 1.6 应用程序中。任何线索都会有所帮助..

最佳答案

os.system(command) 在子 shell 中执行命令(字符串)后返回 exit_status。

最好以以下方式使用:

from subprocess import Popen, PIPE
subproc = Popen(host_str + ".multi.surbl.org", stdout=PIPE, shell=True)
output, errorCode = subproc.communicate()
if errorCode == None:
    black_list = True

关于python - 如何在 python 中正确执行 host 或 dig 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24358535/

相关文章:

javascript - django 奇怪的 Angular js 行为

python - 通过电子邮件请求批准并处理 Python + Django

linux - 在后台具有顺序输入文件的 Shell 脚本

C:打开第二个自定义大小的终端并将其用于输出

python - 将 int 索引设置为 10 位而不是 11 位的 Django 迁移外键

linux - 安装ros map_server时出错

python - 通过 XPath 选择 h2 元素

python - 类和闭包的状态保留

python - 通过用户输入的文件路径导入文件

python - "Unable to find a matching set of capabilities"-- 使用 Python 2.7 的 Selenium