验证域的脚本

Sun*_*ine 5 python sockets dns file-io

目的是从文件中读取域列表,并从头开始执行查找以确认可访问性和解析度。

这是我写的:

#!/usr/bin/python

import os
import socket

f = open('file1.lst', 'r')
s = f.readlines()

for i in s:
    print i
    socket.gethostbyname(i.strip())

f.close()
Run Code Online (Sandbox Code Playgroud)

socket.gethostbyname() 行引发异常。

Eth*_*man 4

for i in s:
    print i
    try:
        socket.gethostbyname(i.strip())
    except socket.gaierror:
        print "unable to get address for", i
Run Code Online (Sandbox Code Playgroud)

如果找不到地址,则会gethostbyname 引发异常(而不是抛出)。这就是 Python 中错误处理的方式。如果您知道如何正确处理错误,则应该使用子句捕获它except

请注意,您还需要一些更多代码来检查连接性。