From b830789f0e0e6525a89d612b90ded0f5225aae73 Mon Sep 17 00:00:00 2001 From: berhsi Date: Fri, 26 Jul 2019 21:33:40 +0200 Subject: [PATCH] client-clear.py: initialer commit initialer commit for a client, who pushs a status of one or zero to the server. if no argument in cli, the client gives a commandline input to read. --- client-clear.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100755 client-clear.py diff --git a/client-clear.py b/client-clear.py new file mode 100755 index 0000000..facd88d --- /dev/null +++ b/client-clear.py @@ -0,0 +1,64 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- + +# file: client-clear.py +# 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. + +import socket +from sys import exit, argv +from kodierung import string2bytes + + +def check_arguments(argv): + + if len(argv) == 1: + value = None + else: + if argv[1].strip() == '0' or argv[1].strip() == '1': + value = argv[1].strip() + print('Set value to {}'.format(value)) + else: + value = None + return value + + +def read_argument(): + + status = None + + while status == None: + buf = input('Enter new status (0/1): ') + if buf == '0' or buf == '1': + status = buf + return buf + + +def main(): + + HOST = '127.0.0.1' + PORT = 64000 + STATUS = None + + if check_arguments(argv) == None: + STATUS = read_argument() + else: + STATUS = check_arguments(argv) + + with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as mySocket: + print('Socket created') + mySocket.connect((HOST, PORT)) + try: + print('Send new status: {}'.format(STATUS)) + bytestream = string2bytes(STATUS) + mySocket.send(bytestream) + except Exception as e: + print('Error: {}'.format(e)) + exit() + + +if __name__ == '__main__': + main()