First commit for client
This commit is contained in:
parent
279eefb431
commit
8fdc8825b3
53
client/log_config.json
Normal file
53
client/log_config.json
Normal file
@ -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"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
83
client/main.py
Normal file
83
client/main.py
Normal file
@ -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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -57,16 +57,13 @@ error = log_server.error
|
|||||||
critical = log_server.critical
|
critical = log_server.critical
|
||||||
|
|
||||||
#### Variables ####
|
#### Variables ####
|
||||||
host = ''
|
HOST = ''
|
||||||
port = 8888
|
PORT = 8888
|
||||||
user = {
|
BUFFER_SIZE = 16384
|
||||||
"fomys": "fomys"
|
|
||||||
}
|
|
||||||
bufferTaille = 16384
|
|
||||||
|
|
||||||
#### Socket ####
|
#### Socket ####
|
||||||
main_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
main_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
main_socket.bind((host, port))
|
main_socket.bind((HOST, PORT))
|
||||||
|
|
||||||
#### Threads ####
|
#### Threads ####
|
||||||
class clientThread(threading.Thread):
|
class clientThread(threading.Thread):
|
||||||
@ -77,7 +74,7 @@ class clientThread(threading.Thread):
|
|||||||
|
|
||||||
:param clentsocket: Client's socket
|
:param clentsocket: Client's socket
|
||||||
:param ip: Client's ip address
|
:param ip: Client's ip address
|
||||||
:param port: Client's connection port
|
:param port: Client's connection PORT
|
||||||
:type clientsocket: socket.socket
|
:type clientsocket: socket.socket
|
||||||
:type ip: str
|
:type ip: str
|
||||||
:type port: int
|
:type port: int
|
||||||
@ -104,7 +101,7 @@ if __name__ == "__main__":
|
|||||||
clients = []
|
clients = []
|
||||||
while True:
|
while True:
|
||||||
main_socket.listen(1) # ecoutes des connections entrantes
|
main_socket.listen(1) # ecoutes des connections entrantes
|
||||||
clientsocket, (ip, port) = main_socket.accept()
|
clientsocket, (ip, PORT) = main_socket.accept()
|
||||||
newClient = clientThread(clientsocket, ip, port)
|
newClient = clientThread(clientsocket, ip, PORT)
|
||||||
newClient.start()
|
newClient.start()
|
||||||
clients.append(newClient)
|
clients.append(newClient)
|
||||||
|
Loading…
Reference in New Issue
Block a user