From 8fdc8825b38c25c92545b4a706e824f1aaca8d8a Mon Sep 17 00:00:00 2001 From: louis chauvet Date: Mon, 12 Nov 2018 20:42:44 +0100 Subject: [PATCH] First commit for client --- client/log_config.json | 53 +++++++++++++++++++++++++++ client/main.py | 83 ++++++++++++++++++++++++++++++++++++++++++ server/main.py | 17 ++++----- 3 files changed, 143 insertions(+), 10 deletions(-) create mode 100644 client/log_config.json create mode 100644 client/main.py diff --git a/client/log_config.json b/client/log_config.json new file mode 100644 index 0000000..4686e03 --- /dev/null +++ b/client/log_config.json @@ -0,0 +1,53 @@ +{ + "version": 1, + "disable_existing_loggers": false, + "formatters": { + "simple": { + "format": "%(asctime)s :: %(name)s :: %(levelname)s :: %(message)s" + } + }, + "handlers": { + "console": { + "class": "logging.StreamHandler", + "level": "DEBUG", + "formatter": "simple", + "stream": "ext://sys.stdout" + }, + "info_file_handler": { + "class": "logging.handlers.RotatingFileHandler", + "level": "INFO", + "formatter": "simple", + "filename": "info.log", + "maxBytes": 1048576, + "backupCount": 20, + "encoding": "utf8" + }, + "error_file_handler": { + "class": "logging.handlers.RotatingFileHandler", + "level": "ERROR", + "formatter": "simple", + "filename": "errors.log", + "maxBytes": 1048576, + "backupCount": 20, + "encoding": "utf8" + } + }, + "loggers": { + "server": { + "level": "DEBUG", + "handlers": [ + "console", + "info_file_handler", + "error_file_handler" + ] + } + }, + "root": { + "level": "INFO", + "handlers": [ + "console", + "info_file_handler", + "error_file_handler" + ] + } +} \ No newline at end of file diff --git a/client/main.py b/client/main.py new file mode 100644 index 0000000..5b057de --- /dev/null +++ b/client/main.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python +# coding: utf8 + +import json +import logging +import os +import socket + + +#### logging #### +# json decoder for int keys +import threading + + +class Decoder(json.JSONDecoder): + def decode(self, s, **kwargs): + result = super().decode(s) # result = super(Decoder, self).decode(s) for Python 2.x + return self._decode(result) + + def _decode(self, o): + if isinstance(o, str): + try: + return int(o) + except ValueError: + return o + elif isinstance(o, dict): + return {k: self._decode(v) for k, v in o.items()} + elif isinstance(o, list): + return [self._decode(v) for v in o] + else: + return o + + +def setup_logging(default_path='log_config.json', default_level=logging.INFO, env_key='LOG_CFG'): + """Setup logging configuration + """ + path = default_path + value = os.getenv(env_key, None) + if value: + path = value + if os.path.exists(path): + with open(path, 'rt') as f: + config = json.load(f) + logging.config.dictConfig(config) + else: + logging.basicConfig(level=default_level) + + +setup_logging() + +log_server = logging.getLogger('server') + +debug = log_server.debug +info = log_server.info +warning = log_server.warning +error = log_server.error +critical = log_server.critical + +#### Variables #### +HOST = '' +PORT = 8888 +BUFFER_SIZE = 16384 + +clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +clientSocket.connect((HOST, PORT)) + +# ET ICI ON MET LE CLIENT + + + + + + + + + + + + + + + + diff --git a/server/main.py b/server/main.py index 62e2566..bee9bf1 100644 --- a/server/main.py +++ b/server/main.py @@ -57,16 +57,13 @@ error = log_server.error critical = log_server.critical #### Variables #### -host = '' -port = 8888 -user = { - "fomys": "fomys" -} -bufferTaille = 16384 +HOST = '' +PORT = 8888 +BUFFER_SIZE = 16384 #### Socket #### main_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -main_socket.bind((host, port)) +main_socket.bind((HOST, PORT)) #### Threads #### class clientThread(threading.Thread): @@ -77,7 +74,7 @@ class clientThread(threading.Thread): :param clentsocket: Client's socket :param ip: Client's ip address - :param port: Client's connection port + :param port: Client's connection PORT :type clientsocket: socket.socket :type ip: str :type port: int @@ -104,7 +101,7 @@ if __name__ == "__main__": clients = [] while True: main_socket.listen(1) # ecoutes des connections entrantes - clientsocket, (ip, port) = main_socket.accept() - newClient = clientThread(clientsocket, ip, port) + clientsocket, (ip, PORT) = main_socket.accept() + newClient = clientThread(clientsocket, ip, PORT) newClient.start() clients.append(newClient)