setstatus.py: changes by code review

better english in comments. the command line arguments are now processed by
argparse. reading vom cli removed. fix bug in exception handling.
This commit is contained in:
Berhsi 2019-10-15 19:21:23 +02:00
parent a0fda2aaa2
commit eb000bff46

View file

@ -5,52 +5,22 @@
# date: 26.07.2019 # date: 26.07.2019
# email: berhsi@web.de # email: berhsi@web.de
# client, who connects to the statusserver at port 10001 to update the # client, that connects to the statusserver at port 10001 to update the
# krautspace door status. If no status is given as argument, he reads from # krautspace door status. allowed arguments are 0 or 1.
# stdin until input is 0 or 1.
import argparse
import socket import socket
import ssl import ssl
from sys import exit, argv import sys
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:
byte_value = None
else:
if argv[1].strip() == '0' or argv[1].strip() == '1':
i = int(argv[1].strip())
print('Set value to {}'.format(i))
byte_value = bytes([i])
else:
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 is None:
buf = input('Enter new status (0/1): ')
if buf == '0' or buf == '1':
status = bytes([int(buf)])
print('Read status: {}'.format(status))
return status
def main(): def main():
parser = argparse.ArgumentParser(description="Set door status of
Krautspace.")
parser.add_argument("status_code", help="status to set", type=int,
choices=(0, 1))
args = parser.parse_args()
print("Status set to {}".format(bytes([args.status_code])))
HOST = 'localhost' HOST = 'localhost'
PORT = 10001 PORT = 10001
@ -61,10 +31,6 @@ def main():
STATUS = None STATUS = None
RESPONSE = None RESPONSE = None
STATUS = check_arguments(argv)
while STATUS is None:
STATUS = read_argument()
context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH, context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH,
cafile=SERVER_CERT) cafile=SERVER_CERT)
context.set_ciphers('EECDH+AESGCM') # only ciphers for tls 1.2 and 1.3 context.set_ciphers('EECDH+AESGCM') # only ciphers for tls 1.2 and 1.3
@ -80,7 +46,7 @@ def main():
print('Connection wrapped with ssl.context') print('Connection wrapped with ssl.context')
conn.settimeout(5.0) conn.settimeout(5.0)
except Exception as e: except Exception as e:
print('Context wrapper failed: [}'.format(e)) print('Context wrapper failed: {}'.format(e))
try: try:
conn.connect((HOST, PORT)) conn.connect((HOST, PORT))
print('Connection established: {}'.format(conn.getpeercert())) print('Connection established: {}'.format(conn.getpeercert()))
@ -88,13 +54,15 @@ def main():
print('Connection timeout') print('Connection timeout')
except Exception as e: except Exception as e:
print('Connection failed: {}'.format(e)) print('Connection failed: {}'.format(e))
exit(1) sys.exit(1)
try: try:
print('Send new status: {}'.format(STATUS)) print('Send new status: {}'.format(STATUS))
conn.send(STATUS) conn.send(STATUS)
except Exception as e: except Exception as e:
print('Error: {}'.format(e)) print('Error: {}'.format(e))
exit(2) sys.exit(2)
try: try:
RESPONSE = conn.recv(1) RESPONSE = conn.recv(1)
print('Server returns: {}'.format(RESPONSE)) print('Server returns: {}'.format(RESPONSE))
@ -105,7 +73,7 @@ def main():
print('Disconnect from server') print('Disconnect from server')
except Exception as e: except Exception as e:
print('Error: {}'.format(e)) print('Error: {}'.format(e))
exit(3) sys.exit(3)
if __name__ == '__main__': if __name__ == '__main__':