loading...

Django view doesn't output correctly

highcenburg profile image Vicente G. Reyes ・1 min read

I've tried the suggestions of the devs who answered the question but the domain still outputs offline even if they're online.

The domains are from an uploaded csv file which will be listed on the database then output an online or offline status depending on the live status of the domain.

Django view doesn't output correctly

0

I have a view which should output if the domain on the database is online or offline.

def pingDomain(request, page):
    page_object = get_object_or_404(Table, page=page)
    try:
        subprocess.check_call(['ping', '-c', '1',  page_object.page])
    except subprocess.CalledProcessError:
        host_online = True
    else:
        host_online = False
    context = {
        'online': host_online,
    }
    return render(request, 'home.html', context)

On the…

Discussion

pic
Editor guide
Collapse
vergeev profile image
Pavel Vergeev

If your end goal is to check website availability, I would suggest making a HEAD request to it, like so:

import requests  # requests library https://github.com/psf/requests

def pingDomain(request, page):
    page_object = get_object_or_404(Table, page=page)
    response = requests.head(url=page_object.page)
    context = {
         'online': response.ok
    }
    return render(request, 'home.html', context)

Here are the advantages:

  • does not rely on the presence of the ping command in the os
  • the above code checks whether the server responds to a HTTP request, but ping checks whether the service responds to a ICMP request
  • the code is shorter :)

Is there any particular reason you've decided to use subprocess.check_call(['ping', '-c', '1', page_object.page])?

Collapse
highcenburg profile image
Vicente G. Reyes Author

I'm not really sure if you understood what I wanted to output on the template since you've mentioned that my end goal was to check the availability of the website or I'm not sure if you meant online/offline with website availability

Collapse
highcenburg profile image
Vicente G. Reyes Author

uhhh this doesn't return the online domains.

Collapse
vergeev profile image
Pavel Vergeev

Wow :O

OK, at this point I can only suggest two things.

  1. Use breakpoint() to inspect the variables inside the view;

  2. Use this code for your reference: github.com/vergeev/17_sites_monito....

Tell me if something new comes up. :)

Thread Thread
highcenburg profile image
Vicente G. Reyes Author

Here's what I did

def pingDomain(request, page, url):
    page_object = get_object_or_404(Table, page=page)
    url = page_object.page
    response = requests.head(url)
    try:
        return requests.head(url).ok
    except requests.exceptions.ConnectionError:
        return False
    context = {
        'online': response.ok
    }
    return render(request, 'home.html', context)

Returns nothing :(

Collapse
highcenburg profile image
Vicente G. Reyes Author

I'll definitely try this!

I just saw

subprocess.check_call(['ping', '-c', '1', page_object.page])
on stackoverflow so I used. Big lesson for me to understand code before using it.
Collapse
rhymes profile image
rhymes

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 Author

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