Mise en constante des messages

This commit is contained in:
Fred Z 2018-02-24 15:57:55 +01:00
parent 1094327e31
commit 9640cd143c
3 changed files with 37 additions and 13 deletions

View File

@ -8,22 +8,27 @@ Networking test, client-server talking script
"""
import socket
hote = "localhost"
port = 12800
HOST = "localhost"
PORT = 12800
RECV_BUFFER = 1024
connexion_avec_serveur = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connexion_avec_serveur.connect((hote, port))
print("Connexion établie avec le serveur sur le port {}".format(port))
MSG_SERVER_CONNECTED = "Serveur connecté @{}:{}"
MSG_CLOSE_CONNECTION = "Connexion vers [{}:{}] fermée"
STOP_COMMAND = "fin"
SERVER_CONNECTION = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
SERVER_CONNECTION.connect((HOST, PORT))
print(MSG_SERVER_CONNECTED.format(HOST, PORT))
msg_a_envoyer = b""
while msg_a_envoyer != b"fin":
while msg_a_envoyer != bytes(STOP_COMMAND, 'utf8'):
msg_a_envoyer = input("> ")
# Peut planter si vous tapez des caractères spéciaux
msg_a_envoyer = msg_a_envoyer.encode()
# On envoie le message
connexion_avec_serveur.send(msg_a_envoyer)
msg_recu = connexion_avec_serveur.recv(1024)
SERVER_CONNECTION.send(msg_a_envoyer)
msg_recu = SERVER_CONNECTION.recv(RECV_BUFFER)
print(msg_recu.decode()) # Là encore, peut planter s'il y a des accents
print("Fermeture de la connexion")
connexion_avec_serveur.close()
SERVER_CONNECTION.close()

View File

@ -14,11 +14,16 @@ PORT = 12800
MSG_NEW_CLIENT = "Nouveau client: {}"
MSG_CLIENT_ID = "Client[{}] {}"
MSG_CLIENT_DISCONNECTED = "Le client {} c'est deconnecté"
MSG_SERVER_STOP = "Arrêt du serveur"
MSG_START_SERVER = "Le serveur écoute à présent sur le PORT {}"
STOP_COMMAND = "fin"
MAIN_CONNECTION = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
MAIN_CONNECTION.bind((HOST, PORT))
MAIN_CONNECTION.listen(5)
print("Le serveur écoute à présent sur le PORT {}".format(PORT))
print(MSG_START_SERVER.format(PORT))
server_on = True
connected_clients = []
@ -68,10 +73,11 @@ while server_on:
print(MSG_CLIENT_ID.format(client.fileno(), msg_recu))
if msg_recu == "fin":
if msg_recu == STOP_COMMAND:
client.send(bytes(MSG_SERVER_STOP, 'utf8'))
server_on = False
print("Fermeture des connexions")
print(MSG_SERVER_STOP)
for client in connected_clients:
client.close()

View File

@ -0,0 +1,13 @@
OOOOOOOOOOOOOOOOOOOOO
OXO O O O
O . O OOO OO OO.OO.O
O O O . O O O O
O O O O O O OO.OOOOO
O O OOOO O O U
O . . O OOO OO.OOOOOOOOOO
O O O O OO OO O
O O OO OO O OOOOOO.OOOO O
O O O O
OOO O OOOOOOOOOOOOOOOOOOOO
O O
OOOOO