2019-07-26 19:43:59 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
# file: server-clear.py
|
|
|
|
# date: 26.07.2019
|
|
|
|
# email: berhsi@web.de
|
|
|
|
|
|
|
|
# server, who listen for connections for localhost, port 64000 and ipv4.
|
|
|
|
|
|
|
|
import socket
|
|
|
|
from time import time, ctime
|
2019-07-27 00:44:53 +02:00
|
|
|
from sys import exit, byteorder
|
2019-07-26 19:43:59 +02:00
|
|
|
|
|
|
|
|
|
|
|
def connection_handle(conn):
|
|
|
|
|
2019-07-27 00:44:53 +02:00
|
|
|
bom = byteorder
|
|
|
|
|
|
|
|
raw_data = conn.recv(1)
|
|
|
|
print('Data recived: {}'.format(raw_data))
|
|
|
|
data = int.from_bytes(raw_data, bom)
|
|
|
|
if data == 0 or data == 1:
|
2019-07-26 19:43:59 +02:00
|
|
|
if update_status(data) == True:
|
2019-07-27 00:44:53 +02:00
|
|
|
conn.send(data.to_bytes(1, bom))
|
2019-07-26 19:43:59 +02:00
|
|
|
else:
|
2019-07-27 00:44:53 +02:00
|
|
|
conn.send(b'\x03')
|
2019-07-26 19:43:59 +02:00
|
|
|
else:
|
|
|
|
print('Invalid argument')
|
2019-07-26 21:57:07 +02:00
|
|
|
conn.send(b'3')
|
2019-07-26 19:43:59 +02:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def update_status(data):
|
|
|
|
|
|
|
|
try:
|
|
|
|
print('Update status ...')
|
2019-07-27 00:44:53 +02:00
|
|
|
if data == 0:
|
2019-07-26 19:43:59 +02:00
|
|
|
status = 'false'
|
|
|
|
else:
|
|
|
|
status = 'true'
|
|
|
|
lastchange = str(time()).split('.')[0]
|
|
|
|
print('Change status: {}: {}'.format(lastchange, status))
|
|
|
|
except Exception as e:
|
|
|
|
print('Error: {}'.format(e))
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
2019-07-27 00:44:53 +02:00
|
|
|
HOST = 'nr18.space'
|
|
|
|
PORT = 10001
|
2019-07-26 19:43:59 +02:00
|
|
|
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as mySocket:
|
|
|
|
print('Socket created')
|
|
|
|
try:
|
|
|
|
mySocket.bind((HOST, PORT))
|
2019-07-26 21:30:58 +02:00
|
|
|
mySocket.listen(5)
|
2019-07-26 19:43:59 +02:00
|
|
|
print('Listen on {} at Port {}'.format(HOST, PORT))
|
|
|
|
except Exception as e:
|
|
|
|
print('Error: unable to bind and listen')
|
|
|
|
print('Error: {}'.format(e))
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
conn, addr = mySocket.accept()
|
|
|
|
print('Connection from {}:{}'.format(addr[0], addr[1]))
|
2019-07-26 21:30:58 +02:00
|
|
|
conn.settimeout(3.0)
|
2019-07-26 19:43:59 +02:00
|
|
|
connection_handle(conn)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print('\b\bExit')
|
|
|
|
exit()
|
|
|
|
except Exception as e:
|
|
|
|
print('Error: {}'.format(e))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|