我正在使用这个代码作为域检查器。但是,我收到一个错误,表示字节不可调用。有人能解释一下怎么解决这个问题吗。
def make_whois_query(domain):
#Execute whois and parse the data to extract specific data
#debug("Sending a WHOIS query for the domain %s" % domain)
try:
p = subprocess.Popen(['whois', domain],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
except Exception as e:
print("Unable to Popen() the whois binary. Exception %s" % e)
sys.exit(1)
try:
whois_data = p.communicate()[0]
except Exception as e:
print("Unable to read from the Popen pipe. Exception %s" % e)
sys.exit(1)
return(parse_whois_data(whois_data))
def parse_whois_data(whois_data):
#Grab the registrar and expiration date from the WHOIS data
debug("Parsing the whois data blob %s" % whois_data)
expiration_date = "00/00/00 00:00:00"
registrar = "Unknown"
for line in whois_data.splitlines():
line 103 if any(expire_string in line for expire_string in EXPIRE_STRINGS):
expiration_date = dateutil.parser.parse(line.partition(": ")[2], ignoretz=True)
if any(registrar_string in line for registrar_string in
REGISTRAR_STRINGS):
registrar = line.split("Registrar:")[1].strip()
return expiration_date, registrar我在make_whois_query中看到了说make_whois_query的错误
返回( parse_whois_data (Whois_data))第103行,parse_whois_data中的whois_data():TypeError:‘字节’对象不可调用
发布于 2019-08-25 15:09:46
请看这一行代码:
whois_data = p.communicate()[0]根据文档
communicate()返回一个元组(stdout_data,stderr_data)。如果以文本模式打开流,则数据将是字符串;否则为字节。
因此,在您的示例中,whois_data是bytes对象,而不是预期的str,因此代码for line in whois_data(): (注意,异常与所提供的代码不对应)失败。要解决这个问题,您应该将whois_data转换为str,或者在文本模式下打开打开的标准输出。
https://stackoverflow.com/questions/57646559
复制相似问题