Jacky
Jacky

Reputation: 41

What is causing TypeError: 'bytes' object is not callable

I'm using this code for domain checker. However I get an error saying bytes not callable. Can someone please explain me how to solve this.

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

I get the error saying line 92, in make_whois_query

return(parse_whois_data(whois_data)) line 103, in parse_whois_data for line in whois_data(): TypeError: 'bytes' object is not callable

Upvotes: 1

Views: 2000

Answers (1)

ingvar
ingvar

Reputation: 4375

Look at this line of code:

whois_data = p.communicate()[0]

According to docs:

communicate() returns a tuple (stdout_data, stderr_data). The data will be strings if streams were opened in text mode; otherwise, bytes.

So in your case whois_data is bytes object, not str as expected, so code for line in whois_data(): (note, exception doesn't correspond to your provided code) fails. To fix this you should convert whois_data to str or open open stdout in text mode.

Upvotes: 1

Related Questions