我想使用 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/