Created homeSender for com with others on the network, this will be changed!

This commit is contained in:
2017-02-10 18:24:10 +01:00
parent 751e408b12
commit 33539e7737
4 changed files with 14 additions and 18 deletions

2
.gitignore vendored
View File

@@ -1 +1 @@
times.txt
__pycache__

BIN
home.db

Binary file not shown.

View File

@@ -1,23 +1,19 @@
#!/usr/bin/env python3
from socket import *
from macLookup import getTimes
import json
# Define the socket communicating will transfered
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind(('', 12001))
def readFile():
with open('/home/kevin/homeCheck/times.txt', 'r') as content_file:
content = content_file.read()
return content
while True:
# Save the message and return address to variables.
message, address = serverSocket.recvfrom(1024)
print(message, address) # Debug print
times = json.dumps(getTimes())
# This should prob be cases and not if's, but works to get the right info user requests.
if (message == 'get_times'):
return_message = readFile()
serverSocket.sendto(bytes(times, 'utf-8'), address)
# Returns message to return address.
serverSocket.sendto(return_message, address)

View File

@@ -7,6 +7,8 @@ from re import findall
from sys import argv
from pprint import pprint
path = "/home/kevin/homeChecker/home.db"
def getOnlineClients():
try:
arpOutput = check_output("sudo arp-scan -l", shell=True)
@@ -25,7 +27,7 @@ def getAddr(c):
return [i[0] for i in c.fetchall()]
def getTimes():
conn = sqlite3.connect('home.db')
conn = sqlite3.connect(path)
c = conn.cursor()
c.execute('SELECT c.name, l.timesince FROM lastonline AS l JOIN clients AS c WHERE l.clientadr=c.adr')
@@ -59,7 +61,7 @@ def convertTime(seconds):
def updateTimes():
curTime = time()
conn = sqlite3.connect('home.db')
conn = sqlite3.connect(path)
c = conn.cursor()
online = list(set(getOnlineClients()) & set(getAddr(c)))
@@ -73,12 +75,10 @@ def updateTimes():
return (online)
if __name__ == '__main__':
if argv[-1] == 'add':
print("Updated following clients:", updateTimes())
elif argv[-1] == 'get':
if argv[-1] == 'get':
pprint(getTimes())
else:
print("Add args 'add' or 'get'")
print("Updated following clients:", updateTimes())