要么想要分析和修复代码,请指出我正确的方向。这么多错误,有些是克服,有些则没有。
程序在Raspberry PI2上运行,应该尝试ping特定的IP地址并返回结果。
编程很新,你可以说!不确定我是否需要ping库,或者可以在没有
的情况下执行此操作import sys
import time
from pushbullet import Pushbullet
import serial
class Users(object):
def __init__(self, name=None, ip=None):
self.name = name
self.ip = ip
self.status = 'out'
pb = Pushbullet("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") #Pushbullet ID removed
userList = []
userList.append(Users("Ali", "192.18.1.14"))
userList.append(Users("Sophie", "192.18.1.9"))
userList.append(Users("TV", "192.18.1.7"))
try:
while True:
print "Checking... " + time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())
for user in userList:
result = os.system ("ping -n 1 " = user.ip)
oldStatus = user.status
if (result == 0):
#What we'll do if a device is detected
if (oldStatus == 'out'):
push = pb.push_note("Home Pi", user.name + " is home")
user.status = 'in'
print user.name + " is home"
else:
#What we'll do if a device is NOT not detected
if (oldStatus == 'in'):
push = pb.push_note("Home Pi", user.name + " has just left")
user.status = 'out'
print user.name + " is out"
print "Next check will be in 30 seconds"
time.sleep(30)
except (KeyboardInterrupt, SystemExit):
答案 0 :(得分:0)
我修改了你的代码,我没有pushbullet。将我的新代码与您之前的代码进行比较,以查看差异和错误
import sys
import time
#from pushbullet import Pushbullet
#import serial
#you need import os
import os
class Users(object):
def __init__(self, name=None, ip=None):
self.name = name
self.ip = ip
self.status = 'out'
#pb = Pushbullet("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") #Pushbullet ID removed
userList = []
userList.append(Users("Notebook", "192.168.1.2"))
userList.append(Users("TV", "192.168.1.4"))
try:
while True:
print "Checking... " + time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())
for user in userList:
#result = os.system ("ping -n 1 " = user.ip)
# to concatenate string you need + sign
#result = os.system("ping -n 1 " + user.ip)
# -n is a wrong option for ubuntu
result = os.system("ping -c 1 " + user.ip)
oldStatus = user.status
if (result == 0):
#What we'll do if a device is detected
if (oldStatus == 'out'):
#push = pb.push_note("Home Pi", user.name + " is home")
user.status = 'in'
print user.name + " is home"
else:
#What we'll do if a device is NOT not detected
if (oldStatus == 'in'):
#push = pb.push_note("Home Pi", user.name + " has just left")
user.status = 'out'
print user.name + " is out"
print "Next check will be in 30 seconds"
time.sleep(30)
#wrong identation
except (KeyboardInterrupt, SystemExit):
sys.exit(0)