2019-07-26 21:33:40 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# file: client-clear.py
|
|
|
|
# date: 26.07.2019
|
|
|
|
# email: berhsi@web.de
|
|
|
|
|
2019-07-27 00:44:53 +02:00
|
|
|
# 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.
|
2019-07-26 21:33:40 +02:00
|
|
|
|
|
|
|
import socket
|
2019-07-27 00:44:53 +02:00
|
|
|
from sys import exit, argv, byteorder
|
2019-07-26 21:33:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
def check_arguments(argv):
|
2019-07-27 00:44:53 +02:00
|
|
|
'''
|
|
|
|
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
|
|
|
|
'''
|
2019-07-26 21:33:40 +02:00
|
|
|
if len(argv) == 1:
|
2019-07-27 00:44:53 +02:00
|
|
|
byte_value = None
|
2019-07-26 21:33:40 +02:00
|
|
|
else:
|
|
|
|
if argv[1].strip() == '0' or argv[1].strip() == '1':
|
2019-07-27 00:44:53 +02:00
|
|
|
i = int(argv[1].strip())
|
|
|
|
print('Set value to {}'.format(i))
|
|
|
|
byte_value = bytes([i])
|
2019-07-26 21:33:40 +02:00
|
|
|
else:
|
2019-07-27 00:44:53 +02:00
|
|
|
byte_value = None
|
|
|
|
return byte_value
|
2019-07-26 21:33:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
def read_argument():
|
2019-07-27 00:44:53 +02:00
|
|
|
'''
|
|
|
|
Reads from stdin until the given value is valid. Convert the given
|
|
|
|
string to a byte value and return this value.
|
|
|
|
return: byte value
|
|
|
|
'''
|
2019-07-26 21:33:40 +02:00
|
|
|
status = None
|
|
|
|
|
|
|
|
while status == None:
|
|
|
|
buf = input('Enter new status (0/1): ')
|
|
|
|
if buf == '0' or buf == '1':
|
2019-07-27 00:44:53 +02:00
|
|
|
status = bytes([int(buf)])
|
|
|
|
print('Read status: {}'.format(status))
|
|
|
|
return status
|
2019-07-26 21:33:40 +02:00
|
|
|
|
|
|
|
|
2019-07-27 16:49:08 +02:00
|
|
|
def main(*status):
|
2019-07-26 21:33:40 +02:00
|
|
|
|
2019-07-27 16:49:08 +02:00
|
|
|
HOST = 'localhost'
|
2019-07-27 00:44:53 +02:00
|
|
|
PORT = 10001
|
|
|
|
BOM = byteorder
|
2019-07-26 21:33:40 +02:00
|
|
|
STATUS = None
|
2019-07-27 00:44:53 +02:00
|
|
|
RESPONSE = None
|
2019-07-26 21:33:40 +02:00
|
|
|
|
2019-07-27 16:49:08 +02:00
|
|
|
if status:
|
|
|
|
STATUS = status[0]
|
|
|
|
STATUS = bytes([STATUS])
|
|
|
|
print('Status: {}'.format(STATUS))
|
2019-07-26 21:33:40 +02:00
|
|
|
else:
|
2019-07-27 16:49:08 +02:00
|
|
|
if len(argv) == 1:
|
|
|
|
STATUS = read_argument()
|
|
|
|
else:
|
|
|
|
STATUS = check_arguments(argv)
|
2019-07-26 21:33:40 +02:00
|
|
|
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as mySocket:
|
|
|
|
print('Socket created')
|
2019-07-27 00:44:53 +02:00
|
|
|
try:
|
|
|
|
mySocket.connect((HOST, PORT))
|
|
|
|
except Exception as e:
|
|
|
|
print('{}'.format(e))
|
|
|
|
exit(1)
|
2019-07-26 21:33:40 +02:00
|
|
|
try:
|
|
|
|
print('Send new status: {}'.format(STATUS))
|
2019-07-27 00:44:53 +02:00
|
|
|
mySocket.send(STATUS)
|
2019-07-26 21:33:40 +02:00
|
|
|
except Exception as e:
|
|
|
|
print('Error: {}'.format(e))
|
2019-07-27 00:44:53 +02:00
|
|
|
exit(2)
|
2019-07-26 22:02:01 +02:00
|
|
|
try:
|
2019-07-27 00:44:53 +02:00
|
|
|
RESPONSE = mySocket.recv(1)
|
|
|
|
print('Server returns: {}'.format(RESPONSE))
|
|
|
|
if RESPONSE == STATUS:
|
|
|
|
print('Status sucessfull updated')
|
|
|
|
else:
|
|
|
|
print('Failed to update status')
|
2019-07-26 22:02:01 +02:00
|
|
|
print('Disconnect from server')
|
2019-07-27 00:44:53 +02:00
|
|
|
except Exception as e:
|
|
|
|
print('Error: {}'.format(e))
|
|
|
|
exit(3)
|
2019-07-26 21:33:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|