I was wondering if I could get some help with the code I pasted below. Right now it runs and completes the ping task however it runs into the following error. Any help would be great as I have been working on it for a while now.
Error:
Server.objects.filter(pk=id[0]).update(online=1)
TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'
Code:
import subprocess
from django.db import models
from networkstats.models import Server
query = Server.objects.values_list('id', 'ip_address')
for ip_address in query:
print 'Server ID: ' + str(ip_address[0])
print 'Server IP: ' + str(ip_address[1])
command = ['ping -t 200 -c 1 ' + ip_address[1]]
ping = subprocess.Popen(command, stdout=subprocess.PIPE, shell = True)
if "100% pocket loss" in ping.stdout.read():
Server.objects.filter(pk=id[0]).update(online=0)
else:
Server.objects.filter(pk=id[0]).update(online=1)
Looks like a typo. Did you want to write
ip_address[0]instead ofid[0]?idis a built-in function, because of it you get this error.Also look at probably better implementation of your code snippet:
This will fetch only needed rows (if there are others) and reduce traffic to the database.