2019-07-26 21:33:40 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-09-14 14:01:52 +02:00
|
|
|
# file: setstatus.py
|
2019-07-26 21:33:40 +02:00
|
|
|
# date: 26.07.2019
|
|
|
|
# email: berhsi@web.de
|
|
|
|
|
2019-10-15 19:21:23 +02:00
|
|
|
# client, that connects to the statusserver at port 10001 to update the
|
|
|
|
# krautspace door status. allowed arguments are 0 or 1.
|
2019-07-26 21:33:40 +02:00
|
|
|
|
2019-10-15 19:21:23 +02:00
|
|
|
import argparse
|
2019-07-26 21:33:40 +02:00
|
|
|
import socket
|
2019-07-29 18:27:53 +02:00
|
|
|
import ssl
|
2019-10-15 19:21:23 +02:00
|
|
|
import sys
|
2019-07-26 21:33:40 +02:00
|
|
|
|
|
|
|
|
2019-09-19 09:44:52 +02:00
|
|
|
def main():
|
2019-10-15 19:21:23 +02:00
|
|
|
parser = argparse.ArgumentParser(description="Set door status of
|
|
|
|
Krautspace.")
|
|
|
|
parser.add_argument("status_code", help="status to set", type=int,
|
|
|
|
choices=(0, 1))
|
|
|
|
args = parser.parse_args()
|
|
|
|
print("Status set to {}".format(bytes([args.status_code])))
|
2019-07-26 21:33:40 +02:00
|
|
|
|
2019-07-29 18:27:53 +02:00
|
|
|
HOST = 'localhost'
|
2019-07-27 00:44:53 +02:00
|
|
|
PORT = 10001
|
2019-07-29 18:27:53 +02:00
|
|
|
SERVER_NAME = 'server.status.kraut.space'
|
|
|
|
CLIENT_CERT = './certs/client.crt'
|
|
|
|
CLIENT_KEY = './certs/client.key'
|
|
|
|
SERVER_CERT = './certs/server.crt'
|
2019-07-26 21:33:40 +02:00
|
|
|
STATUS = None
|
2019-07-27 00:44:53 +02:00
|
|
|
RESPONSE = None
|
2019-07-26 21:33:40 +02:00
|
|
|
|
2019-09-19 09:44:52 +02:00
|
|
|
context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH,
|
|
|
|
cafile=SERVER_CERT)
|
2019-09-19 10:36:48 +02:00
|
|
|
context.set_ciphers('EECDH+AESGCM') # only ciphers for tls 1.2 and 1.3
|
|
|
|
context.options |= getattr(ssl._ssl, 'OP_NO_COMPRESSION', 0)
|
2019-09-19 09:44:52 +02:00
|
|
|
context.load_cert_chain(certfile=CLIENT_CERT, keyfile=CLIENT_KEY)
|
2019-07-29 18:27:53 +02:00
|
|
|
print('SSL context created')
|
|
|
|
|
2019-07-26 21:33:40 +02:00
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as mySocket:
|
|
|
|
print('Socket created')
|
2019-07-27 00:44:53 +02:00
|
|
|
try:
|
2019-09-19 09:44:52 +02:00
|
|
|
conn = context.wrap_socket(mySocket, server_side=False,
|
|
|
|
server_hostname=SERVER_NAME)
|
2019-07-29 18:27:53 +02:00
|
|
|
print('Connection wrapped with ssl.context')
|
2019-09-14 14:01:52 +02:00
|
|
|
conn.settimeout(5.0)
|
2019-07-29 18:27:53 +02:00
|
|
|
except Exception as e:
|
2019-10-15 19:21:23 +02:00
|
|
|
print('Context wrapper failed: {}'.format(e))
|
2019-07-29 18:27:53 +02:00
|
|
|
try:
|
|
|
|
conn.connect((HOST, PORT))
|
2019-09-14 14:01:52 +02:00
|
|
|
print('Connection established: {}'.format(conn.getpeercert()))
|
|
|
|
except socket.timeout:
|
|
|
|
print('Connection timeout')
|
2019-07-27 00:44:53 +02:00
|
|
|
except Exception as e:
|
2019-09-14 14:01:52 +02:00
|
|
|
print('Connection failed: {}'.format(e))
|
2019-10-15 19:21:23 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
2019-07-26 21:33:40 +02:00
|
|
|
try:
|
|
|
|
print('Send new status: {}'.format(STATUS))
|
2019-07-29 18:27:53 +02:00
|
|
|
conn.send(STATUS)
|
2019-07-26 21:33:40 +02:00
|
|
|
except Exception as e:
|
|
|
|
print('Error: {}'.format(e))
|
2019-10-15 19:21:23 +02:00
|
|
|
sys.exit(2)
|
|
|
|
|
2019-07-26 22:02:01 +02:00
|
|
|
try:
|
2019-07-29 18:27:53 +02:00
|
|
|
RESPONSE = conn.recv(1)
|
2019-07-27 00:44:53 +02:00
|
|
|
print('Server returns: {}'.format(RESPONSE))
|
|
|
|
if RESPONSE == STATUS:
|
|
|
|
print('Status sucessfull updated')
|
|
|
|
else:
|
|
|
|
print('Failed to update status')
|
2019-07-26 22:02:01 +02:00
|
|
|
print('Disconnect from server')
|
2019-07-27 00:44:53 +02:00
|
|
|
except Exception as e:
|
|
|
|
print('Error: {}'.format(e))
|
2019-10-15 19:21:23 +02:00
|
|
|
sys.exit(3)
|
2019-07-26 21:33:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|