client and server works with bytes and nr18.space now
both parts now uses byteorder, int.from_bytes() and data.to_bytes(). to compare values, raw data are cast to integer. furthermore encode() is nowhere used. at the moment the server listen at nr18.space at port 10001.
This commit is contained in:
parent
5522e04840
commit
0b91b007db
2 changed files with 57 additions and 35 deletions
|
@ -5,43 +5,57 @@
|
||||||
# date: 26.07.2019
|
# date: 26.07.2019
|
||||||
# email: berhsi@web.de
|
# email: berhsi@web.de
|
||||||
|
|
||||||
# client, who connects to localhost port 64000. If no status is given, he
|
# client, who connects to nr18.space at port 10001 to update the krautspace
|
||||||
# reads from commandline until input is 0 or 1.
|
# door status. If no status is given as argument, he reads from stdin until
|
||||||
|
# input is 0 or 1.
|
||||||
|
|
||||||
import socket
|
import socket
|
||||||
from sys import exit, argv
|
from sys import exit, argv, byteorder
|
||||||
from kodierung import string2bytes
|
|
||||||
|
|
||||||
|
|
||||||
def check_arguments(argv):
|
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:
|
if len(argv) == 1:
|
||||||
value = None
|
byte_value = None
|
||||||
else:
|
else:
|
||||||
if argv[1].strip() == '0' or argv[1].strip() == '1':
|
if argv[1].strip() == '0' or argv[1].strip() == '1':
|
||||||
value = argv[1].strip()
|
i = int(argv[1].strip())
|
||||||
print('Set value to {}'.format(value))
|
print('Set value to {}'.format(i))
|
||||||
|
byte_value = bytes([i])
|
||||||
else:
|
else:
|
||||||
value = None
|
byte_value = None
|
||||||
return value
|
return byte_value
|
||||||
|
|
||||||
|
|
||||||
def read_argument():
|
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
|
status = None
|
||||||
|
|
||||||
while status == None:
|
while status == None:
|
||||||
buf = input('Enter new status (0/1): ')
|
buf = input('Enter new status (0/1): ')
|
||||||
if buf == '0' or buf == '1':
|
if buf == '0' or buf == '1':
|
||||||
status = buf
|
status = bytes([int(buf)])
|
||||||
return buf
|
print('Read status: {}'.format(status))
|
||||||
|
return status
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
HOST = '127.0.0.1'
|
HOST = 'nr18.space'
|
||||||
PORT = 64000
|
PORT = 10001
|
||||||
|
BOM = byteorder
|
||||||
STATUS = None
|
STATUS = None
|
||||||
|
RESPONSE = None
|
||||||
|
|
||||||
if check_arguments(argv) == None:
|
if check_arguments(argv) == None:
|
||||||
STATUS = read_argument()
|
STATUS = read_argument()
|
||||||
|
@ -50,20 +64,28 @@ def main():
|
||||||
|
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as mySocket:
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as mySocket:
|
||||||
print('Socket created')
|
print('Socket created')
|
||||||
mySocket.connect((HOST, PORT))
|
try:
|
||||||
|
mySocket.connect((HOST, PORT))
|
||||||
|
except Exception as e:
|
||||||
|
print('{}'.format(e))
|
||||||
|
exit(1)
|
||||||
try:
|
try:
|
||||||
print('Send new status: {}'.format(STATUS))
|
print('Send new status: {}'.format(STATUS))
|
||||||
bytestream = string2bytes(STATUS)
|
mySocket.send(STATUS)
|
||||||
mySocket.send(bytestream)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('Error: {}'.format(e))
|
print('Error: {}'.format(e))
|
||||||
exit()
|
exit(2)
|
||||||
try:
|
try:
|
||||||
res = mySocket.recv(1)
|
RESPONSE = mySocket.recv(1)
|
||||||
print('Server returns: {}'.format(res.decode()))
|
print('Server returns: {}'.format(RESPONSE))
|
||||||
finally:
|
if RESPONSE == STATUS:
|
||||||
mySocket.close()
|
print('Status sucessfull updated')
|
||||||
|
else:
|
||||||
|
print('Failed to update status')
|
||||||
print('Disconnect from server')
|
print('Disconnect from server')
|
||||||
|
except Exception as e:
|
||||||
|
print('Error: {}'.format(e))
|
||||||
|
exit(3)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
@ -8,17 +8,21 @@
|
||||||
|
|
||||||
import socket
|
import socket
|
||||||
from time import time, ctime
|
from time import time, ctime
|
||||||
from sys import exit
|
from sys import exit, byteorder
|
||||||
|
|
||||||
|
|
||||||
def connection_handle(conn):
|
def connection_handle(conn):
|
||||||
|
|
||||||
data = conn.recv(1).decode()
|
bom = byteorder
|
||||||
if data == '0' or data == '1':
|
|
||||||
|
raw_data = conn.recv(1)
|
||||||
|
print('Data recived: {}'.format(raw_data))
|
||||||
|
data = int.from_bytes(raw_data, bom)
|
||||||
|
if data == 0 or data == 1:
|
||||||
if update_status(data) == True:
|
if update_status(data) == True:
|
||||||
conn.send(data.encode())
|
conn.send(data.to_bytes(1, bom))
|
||||||
else:
|
else:
|
||||||
conn.send(b'3')
|
conn.send(b'\x03')
|
||||||
else:
|
else:
|
||||||
print('Invalid argument')
|
print('Invalid argument')
|
||||||
conn.send(b'3')
|
conn.send(b'3')
|
||||||
|
@ -30,7 +34,7 @@ def update_status(data):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
print('Update status ...')
|
print('Update status ...')
|
||||||
if data == '0':
|
if data == 0:
|
||||||
status = 'false'
|
status = 'false'
|
||||||
else:
|
else:
|
||||||
status = 'true'
|
status = 'true'
|
||||||
|
@ -44,8 +48,8 @@ def update_status(data):
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
HOST = '127.0.0.1'
|
HOST = 'nr18.space'
|
||||||
PORT = 64000
|
PORT = 10001
|
||||||
|
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as mySocket:
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as mySocket:
|
||||||
print('Socket created')
|
print('Socket created')
|
||||||
|
@ -57,7 +61,6 @@ def main():
|
||||||
print('Error: unable to bind and listen')
|
print('Error: unable to bind and listen')
|
||||||
print('Error: {}'.format(e))
|
print('Error: {}'.format(e))
|
||||||
while True:
|
while True:
|
||||||
conn = None
|
|
||||||
try:
|
try:
|
||||||
conn, addr = mySocket.accept()
|
conn, addr = mySocket.accept()
|
||||||
print('Connection from {}:{}'.format(addr[0], addr[1]))
|
print('Connection from {}:{}'.format(addr[0], addr[1]))
|
||||||
|
@ -68,9 +71,6 @@ def main():
|
||||||
exit()
|
exit()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('Error: {}'.format(e))
|
print('Error: {}'.format(e))
|
||||||
finally:
|
|
||||||
if conn:
|
|
||||||
conn.close()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in a new issue