想要的代码分析和修复或指出我在正确的方向,请。这么多的错误,有的克服了,有的没有。
程序运行在覆盆子PI2上,应该尝试和ping特定的IP地址,并返回结果。
这是编程的新手,你大概能看出来!不确定是否需要ping库,或者是否可以不使用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):发布于 2016-01-25 03:28:45
我修改了你的代码才能工作,我没有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)https://stackoverflow.com/questions/34980014
复制相似问题