doorstatus/client-clear.py

71 lines
1.6 KiB
Python
Raw Normal View History

#!/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()
try:
res = mySocket.recv(1)
print('Server returns: {}'.format(res.decode()))
finally:
mySocket.close()
print('Disconnect from server')
if __name__ == '__main__':
main()