1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| # -*- coding: utf-8 -*-
HOST = '127.0.0.1 '
PORT = 40000
import socket, sys, threading
class ThreadClient(threading.Thread):
def __init__(self, conn):
threading.Thread.__init__(self)
self.connexion = conn
def run(self):
nom = self.getName()
while 1:
msgClient = self.connexion.recv(1024)
if msgClient.upper() == "FIN " or msgClient == " ":
break
message = "%s> %s " % (nom, msgClient)
print message
for cle in conn_client:
if cle != nom:
conn_client[cle].send(message)
del conn_client[nom]
chaine =conn_client[nom]
fichier = open("log.txt","r")
for ligne in fichier:
if chaine in ligne:
ligne2=fichier.readline(ligne)
contenu=contenu.replace(ligne2, '')
fichier.close()
print "Client %s déconnecté. " % nom
mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
mySocket.bind((HOST, PORT))
except socket.error:
print "La liaison du socket à l 'adresse choisie a échoué. "
sys.exit()
print "Serveur prêt, en attente de requêtes ... "
mySocket.listen(5)
conn_client = {}
while 1:
connexion, adresse = mySocket.accept()
th = ThreadClient(connexion)
th.start()
it = th.getName() # identifiant du thread
conn_client[it] = connexion
az=""+ it +"\n"
print "Client %s connecté, adresse IP %s, port %s. " %\
(it, adresse[0], adresse[1])
file=open('log.txt','a')
file.write(az)
file.close()
connexion.send( "Vous êtes connecté. Envoyez vos messages. ") |
Partager