forked from Krautspace/doorstatus
client and server works with bytes and nr18.space now
both parts now uses byteorder, int.from_bytes() and data.to_bytes(). to compare values, raw data are cast to integer. furthermore encode() is nowhere used. at the moment the server listen at nr18.space at port 10001.
This commit is contained in:
parent
5522e04840
commit
0b91b007db
2 changed files with 57 additions and 35 deletions
|
@ -5,43 +5,57 @@
|
|||
# date: 26.07.2019
|
||||
# email: berhsi@web.de
|
||||
|
||||
# client, who connects to localhost port 64000. If no status is given, he
|
||||
# reads from commandline until input is 0 or 1.
|
||||
# client, who connects to nr18.space at port 10001 to update the krautspace
|
||||
# door status. If no status is given as argument, he reads from stdin until
|
||||
# input is 0 or 1.
|
||||
|
||||
import socket
|
||||
from sys import exit, argv
|
||||
from kodierung import string2bytes
|
||||
from sys import exit, argv, byteorder
|
||||
|
||||
|
||||
def check_arguments(argv):
|
||||
|
||||
'''
|
||||
Checks length and validity of command line argument vectors. If there is
|
||||
no argument or argument is not valid, it returns None. Otherwise it
|
||||
converts the string value into a byte value.
|
||||
param 1: array of strings
|
||||
return: None or byte value
|
||||
'''
|
||||
if len(argv) == 1:
|
||||
value = None
|
||||
byte_value = None
|
||||
else:
|
||||
if argv[1].strip() == '0' or argv[1].strip() == '1':
|
||||
value = argv[1].strip()
|
||||
print('Set value to {}'.format(value))
|
||||
i = int(argv[1].strip())
|
||||
print('Set value to {}'.format(i))
|
||||
byte_value = bytes([i])
|
||||
else:
|
||||
value = None
|
||||
return value
|
||||
byte_value = None
|
||||
return byte_value
|
||||
|
||||
|
||||
def read_argument():
|
||||
|
||||
'''
|
||||
Reads from stdin until the given value is valid. Convert the given
|
||||
string to a byte value and return this value.
|
||||
return: byte value
|
||||
'''
|
||||
status = None
|
||||
|
||||
while status == None:
|
||||
buf = input('Enter new status (0/1): ')
|
||||
if buf == '0' or buf == '1':
|
||||
status = buf
|
||||
return buf
|
||||
status = bytes([int(buf)])
|
||||
print('Read status: {}'.format(status))
|
||||
return status
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
HOST = '127.0.0.1'
|
||||
PORT = 64000
|
||||
HOST = 'nr18.space'
|
||||
PORT = 10001
|
||||
BOM = byteorder
|
||||
STATUS = None
|
||||
RESPONSE = None
|
||||
|
||||
if check_arguments(argv) == None:
|
||||
STATUS = read_argument()
|
||||
|
@ -50,20 +64,28 @@ def main():
|
|||
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as mySocket:
|
||||
print('Socket created')
|
||||
try:
|
||||
mySocket.connect((HOST, PORT))
|
||||
except Exception as e:
|
||||
print('{}'.format(e))
|
||||
exit(1)
|
||||
try:
|
||||
print('Send new status: {}'.format(STATUS))
|
||||
bytestream = string2bytes(STATUS)
|
||||
mySocket.send(bytestream)
|
||||
mySocket.send(STATUS)
|
||||
except Exception as e:
|
||||
print('Error: {}'.format(e))
|
||||
exit()
|
||||
exit(2)
|
||||
try:
|
||||
res = mySocket.recv(1)
|
||||
print('Server returns: {}'.format(res.decode()))
|
||||
finally:
|
||||
mySocket.close()
|
||||
RESPONSE = mySocket.recv(1)
|
||||
print('Server returns: {}'.format(RESPONSE))
|
||||
if RESPONSE == STATUS:
|
||||
print('Status sucessfull updated')
|
||||
else:
|
||||
print('Failed to update status')
|
||||
print('Disconnect from server')
|
||||
except Exception as e:
|
||||
print('Error: {}'.format(e))
|
||||
exit(3)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
@ -8,17 +8,21 @@
|
|||
|
||||
import socket
|
||||
from time import time, ctime
|
||||
from sys import exit
|
||||
from sys import exit, byteorder
|
||||
|
||||
|
||||
def connection_handle(conn):
|
||||
|
||||
data = conn.recv(1).decode()
|
||||
if data == '0' or data == '1':
|
||||
bom = byteorder
|
||||
|
||||
raw_data = conn.recv(1)
|
||||
print('Data recived: {}'.format(raw_data))
|
||||
data = int.from_bytes(raw_data, bom)
|
||||
if data == 0 or data == 1:
|
||||
if update_status(data) == True:
|
||||
conn.send(data.encode())
|
||||
conn.send(data.to_bytes(1, bom))
|
||||
else:
|
||||
conn.send(b'3')
|
||||
conn.send(b'\x03')
|
||||
else:
|
||||
print('Invalid argument')
|
||||
conn.send(b'3')
|
||||
|
@ -30,7 +34,7 @@ def update_status(data):
|
|||
|
||||
try:
|
||||
print('Update status ...')
|
||||
if data == '0':
|
||||
if data == 0:
|
||||
status = 'false'
|
||||
else:
|
||||
status = 'true'
|
||||
|
@ -44,8 +48,8 @@ def update_status(data):
|
|||
|
||||
def main():
|
||||
|
||||
HOST = '127.0.0.1'
|
||||
PORT = 64000
|
||||
HOST = 'nr18.space'
|
||||
PORT = 10001
|
||||
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as mySocket:
|
||||
print('Socket created')
|
||||
|
@ -57,7 +61,6 @@ def main():
|
|||
print('Error: unable to bind and listen')
|
||||
print('Error: {}'.format(e))
|
||||
while True:
|
||||
conn = None
|
||||
try:
|
||||
conn, addr = mySocket.accept()
|
||||
print('Connection from {}:{}'.format(addr[0], addr[1]))
|
||||
|
@ -68,9 +71,6 @@ def main():
|
|||
exit()
|
||||
except Exception as e:
|
||||
print('Error: {}'.format(e))
|
||||
finally:
|
||||
if conn:
|
||||
conn.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
Loading…
Reference in a new issue