diff --git a/isc b/isc new file mode 100755 index 0000000..ab35ead --- /dev/null +++ b/isc @@ -0,0 +1,79 @@ +#!/usr/bin/env python + +#import pprint +import datetime +import socket +import sys +import time + +now = int(time.time()) + + +// TODO: enable interactive mode via param (-i) +// make logging optional (-l) + +def log(string): + current = datetime.datetime.now() + print('[' + str(current) + '] ' + string, end='') + with open(str(now) + '-client.log', 'a') as f: + f.write('[' + str(current) + '] ' + string) + + +def connect(host, port): + print('[' + str(datetime.datetime.now()) + '] + 'Connecting to: ' + host + ':' + str(port)) + client = socket.socket() + client.settimeout(2) + while True: + try: + client.connect((host, port)) + break + except Exception as err: + print('[' + str(datetime.datetime.now()) + '] ' + 'Connection error: {0}'.format(err)) + print('[' + str(datetime.datetime.now()) + '] ' + 'Retrying...') + time.sleep(1) + + return client + + +def client(host, port): + + #pp = pprint.PrettyPrinter(indent=4) + + client = connect(host, port) + + #message = input(" -> ") # take input + + while True: + #client.send(message.encode()) + #print(client) + + try: + data = client.recv(1024).decode() + log('Received from server: ' + data) + + except Exception as err: + print('[' + str(datetime.datetime.now()) + '] ' + 'Connection error: {0}'.format(err)) + #client = connect(host, port) + while True: + try: + print('[' + str(datetime.datetime.now()) + '] ' + 'Reconnecting...') + client = connect(host, port) + break + except Exception as err: + print('[' + str(datetime.datetime.now()) + '] ' + 'Reconnect error: {0}\n'.format(err)) + sleep(1) + + #message = input(" -> ") # again take input + print('[' + str(datetime.datetime.now()) + '] ' + 'Closing connection...') + client.close() + + +if __name__ == '__main__': + if len(sys.argv) < 3: + print('No host and or port set! Aborting...\n\nCommand:\n./client.py IP PORT\nExample:\n ./client.py 127.0.0.1 2342') + exit(-1) + else: + host = sys.argv[1] + port = sys.argv[2] + client(host, int(port)) +