forked from Krautspace/doorstatus
setstatus.py: its possible now to pass a integer to main()
to perform a stresstest its possible now to pass an integer as argument to main()
This commit is contained in:
parent
868702eb8d
commit
777c320ce4
1 changed files with 10 additions and 5 deletions
|
@ -1,92 +0,0 @@
|
|||
#!/usr/bin/python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# file: client-clear.py
|
||||
# date: 26.07.2019
|
||||
# email: berhsi@web.de
|
||||
|
||||
# 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, 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:
|
||||
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 == 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():
|
||||
|
||||
HOST = 'nr18.space'
|
||||
PORT = 10001
|
||||
BOM = byteorder
|
||||
STATUS = None
|
||||
RESPONSE = 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')
|
||||
try:
|
||||
mySocket.connect((HOST, PORT))
|
||||
except Exception as e:
|
||||
print('{}'.format(e))
|
||||
exit(1)
|
||||
try:
|
||||
print('Send new status: {}'.format(STATUS))
|
||||
mySocket.send(STATUS)
|
||||
except Exception as e:
|
||||
print('Error: {}'.format(e))
|
||||
exit(2)
|
||||
try:
|
||||
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__':
|
||||
main()
|
Loading…
Add table
Add a link
Reference in a new issue