473,408 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

multithreading python serveur

5
Bonjour,
Voici mon code que j'ai utilisé pour faire une communication entre un serveur et des clients.
Pouvez vous m'aider à l'améliorer pour que l'affichage soit meilleur dans les deux coté (je veux que le nom du thread soit afficher dans le serveur) et aussi il ya un probleme dans le coté client où l'envoye de données est répeté après "s>" je ne sait pas pourquoi. Vous pouvez l'executer sous dos.
Merci.
J'attends vos modifications et commentaires.:)

code serveur:
from socket import *
import socket, sys, threading
import SocketServer

class GestionnaireEcho(SocketServer.BaseRequestHandler):

def handle(self):
print "Connexion de ", self.client_address
while True:
donneesRecues = self.request.recv(8192)
if not donneesRecues: break
print "Donnees Recues : ", donneesRecues
self.request.sendall(donneesRecues)
msg= raw_input("S> ")
print "Donnees envoyees : ", msg
self.request.send(msg)

print "Deconnexion de ", self.client_address

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)
message = "%s> %s" % (nom, msgClient)
print message

for cle in conn_client:
if cle != nom:
conn_client[cle].send(message)

self.connexion.close()
del conn_client[nom]
print "Client %s deconnecte." % nom
mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
serveur = SocketServer.ThreadingTCPServer(('127.0.0.1',84), GestionnaireEcho)
except socket.error:
print "La liaison du socket a l'adresse choisie a echoue."
sys.exit()

print "En attente..!!!!"
conn_client = {}

serveur.serve_forever()



code client

# Definition d"un client reseau gerant en parallele l'emission
# et la reception des messages (utilisation de 2 THREADS).

host = '127.0.0.1'
port = 84

import socket, sys, threading

class ThreadReception(threading.Thread):
"""objet thread gerant la reception des messages"""
def __init__(self, conn):
threading.Thread.__init__(self)
self.connexion = conn # ref. du socket de connexion

def run(self):
while 1:
message_recu = self.connexion.recv(1024)
print "Ser>" + message_recu

# Le thread <reception> se termine ici.
# On force la fermeture du thread <emission> :

print "Client arrete. Connexion interrompue."
self.connexion.close()

class ThreadEmission(threading.Thread):

"""objet thread gerant l'emission des messages"""
def __init__(self, conn):
threading.Thread.__init__(self)
self.connexion = conn # ref. du socket de connexion

def run(self):
message_final=""
while 1:
message_final=""
message_emis = raw_input()
message_final = message_final + " \n "+ message_emis
self.connexion.send(message_final)



# Programme principal - etablissement de la connexion :
connexion = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
connexion.connect((host, port))
except socket.error:
print "La connexion a echoue."
sys.exit()
print "Connexion etablie avec le serveur."

# Dialogue avec le serveur : on lance deux threads pour gerer
# independamment l'emission et la reception des messages :

th_E = ThreadEmission(connexion)
th_R = ThreadReception(connexion)
th_E.start()
th_R.start()
Mar 25 '08 #1
1 2546
Laharl
849 Expert 512MB
Parlez-vous Anglais?
Mar 25 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Chris | last post by:
Bonjour, Plusieurs fichiers PHP d'un programme open source de compteur de visites viennent de se faire hacker sur mon serveur (hébergement mutualisé chez un fournisseur d'accès). Le hacker a...
1
by: abhinav | last post by:
Hi guys.I have read that one cannot perform true multithreading in python due to global interpreter lock mechanism.Suppose i have to implement a crawler on a say cluster system like clusterknoppix...
7
by: Sébastien Ramage | last post by:
I've an idea and I've made some search but I found nothing really interesting. There is somebody who have (or can help me to) try to developp a python plugin for web browser just like java ?? I...
20
by: S.Mohideen | last post by:
Hi Folks, Python is praised about - me too. But at one instance it fails. It fails to behave as a true multi-threaded application. That means utilizing all the CPUs parallely in the SMP...
3
by: =?ISO-8859-2?Q?Krzysztof_W=B3odarczyk?= | last post by:
Hi, I think I've found a bug in Python/C API and multithreading. I'm currently creating an intrusion detection system based on mobile agents. I have an AgentPlatform (C/C++) and 2 agents on...
0
by: eliseo | last post by:
Hello, I want to make the same application : python Serveur and J2me Client whit ssl socket. I would like to know if you have solved the problem? Can you show me the final version of python...
0
by: tsic | last post by:
Bonjour, Voilà, j'ai débuté recemment avec python et je veux faire une connexion client serveur (windows) par l'intermédiaire d'une interface QT Designer v 4. j'ai choisi ERIC v 4 comme IDE. ...
3
by: tsic | last post by:
Bonjour, SVP aidez moi à trouver le code du serveur de connexion. j'ai déjà testé celui de windows et ça a marché. Mais mon encadreur demande un serveur qui soit mis sous linux et que le client...
1
by: tsic | last post by:
Bonjour, je suis bloqué ilya preque 2 semaines dans le code suivant. je veut que le client et le serveur envoyent leurs message d'une maniere comme msn. je crois qu'ilya quelque chose à ajouter dans...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.