forked from Krautspace/doorstatus
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.
This commit is contained in:
parent
4bbd2aed54
commit
b830789f0e
1 changed files with 64 additions and 0 deletions
64
client-clear.py
Executable file
64
client-clear.py
Executable file
|
@ -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()
|
Loading…
Reference in a new issue