首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >导致TypeError的原因:“字节”对象不可调用

问导致TypeError的原因:“字节”对象不可调用
EN

Stack Overflow用户
提问于 2019-08-25 13:44:04
回答 1查看 1.5K关注 0票数 1

我正在使用这个代码作为域检查器。但是,我收到一个错误,表示字节不可调用。有人能解释一下怎么解决这个问题吗。

代码语言:javascript
复制
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:‘字节’对象不可调用

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-25 15:09:46

请看这一行代码:

代码语言:javascript
复制
whois_data = p.communicate()[0]

根据文档

communicate()返回一个元组(stdout_data,stderr_data)。如果以文本模式打开流,则数据将是字符串;否则为字节。

因此,在您的示例中,whois_data是bytes对象,而不是预期的str,因此代码for line in whois_data(): (注意,异常与所提供的代码不对应)失败。要解决这个问题,您应该将whois_data转换为str,或者在文本模式下打开打开的标准输出。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57646559

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档