<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def cmd_extract_hostname(infile, check, verbose, jsonout):
 """Extract hostnames from a file or stdin. Example: \b $ cat /var/log/some.log | habu.extract.hostname www.google.com ibm.com fileserver.redhat.com """


    if verbose:
        logging.basicConfig(level=logging.INFO, format='%(message)s')

    data = infile.read()

    result = extract_hostname(data)

    if check:
        logging.info('Checking against DNS...')
        for candidate in result:
            try:
                socket.getaddrinfo(candidate, None)
            except socket.gaierror:
                result.remove(candidate)

    if jsonout:
        print(json.dumps(result, indent=4))
    else:
        print('\n'.join(result))