DEV Community

Discussion on: Django view doesn't output correctly

Collapse
 
rhymes profile image
rhymes • Edited

Hi, aside from the logic being incorrect, see the correction in this answer:

I believe what you're trying to achieve is the following:

def pingDomain(request, page):
    page_object = get_object_or_404(Table, page=page)
    try:
        subprocess.check_output(['ping', '-c', '1',  page_object.page])
        host_online = True
    except subprocess.CalledProcessError:
        host_online = False

    context = {
        'online': host_online,
    }
    return render(request, 'home.html', context)

Reason being I've changed your logic is because if the…

I see a few additional problems:

  • some domains, even if online, won't respond to ICMP packets (what ping uses for the echo), which means that you'll have false negatives :)

  • another bug in your logic is that it assumes that any type of response from ping, even unreachable domains, will result in the host marked online

I suggest you add a call to breakpoint() to the line before the subprocess call and check what's going on manually

Collapse
 
highcenburg profile image
Vicente G. Reyes

Hey @rhymes ! Thanks! I'll check on this.