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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
#!/usr/bin/env python
# -*- coding: ISO8859-1 -*-
#
#
import xmlrpclib
class MyMarshall(xmlrpclib.Marshaller):
def __init__(self, encoding=None, allow_none=0):
self.memo = {}
self.data = None
self.encoding = encoding
self.allow_none = 1
xmlrpclib.Marshaller = MyMarshall
from SimpleXMLRPCServer import SimpleXMLRPCServer
class appsvr(object):
def __init__(self):
object.__init__(self)
self.clidial = {}
appserver = SimpleXMLRPCServer(("localhost", 8000))
print "Serveur en écoute sur le port 8000..."
appserver.register_function(self.reg_me, "reg_me")
appserver.register_function(self.dial_us, "dial_us")
appserver.register_function(self.give_me, "give_me")
appserver.register_function(self.unreg_me, "unreg_me")
appserver.register_function(self.clear_me, "clear_me")
appserver.register_function(self.list_me, "list_me")
try:
print 'Utilisez Control-C pour finir'
appserver.serve_forever()
except KeyboardInterrupt:
print 'Fin'
def reg_me(self, cliname):
# Enregistrement d'un client et création de sa file d'attente
if cliname in self.clidial:
print "client", cliname, "déjà enregistré"
return False
else:
self.clidial[cliname]=[]
print 'Enregistrement du client', cliname
print self.clidial
return True
def dial_us(self, cliname, msg):
# Ajout d'un message pour un client
if cliname in self.clidial:
self.clidial[cliname].append(msg)
print "Réception d'un message pour", msg[0], "avec la variable", msg[1], "qui a pour valeur", msg[2]
return True
else:
print 'Client inconnu'
return False
def give_me(self, cliname):
# Retourne les messages a un client
if cliname in self.clidial:
print "Retourne", self.clidial[cliname]
return self.clidial[cliname]
else: return None
def clear_me(self, cliname):
# Supprime les messages d'un client
if cliname in self.clidial:
print "Messages pour le client", cliname, "supprimés"
self.clidial[cliname]=[]
return True
else:
print 'Client inconnu'
return False
def unreg_me(self, cliname):
# Supprime un client
if cliname in self.clidial:
print "Supprime", self.clidial[cliname]
del self.clidial[cliname]
return True
else:
print 'Client inconnu'
return False
def list_me(self):
return self.clidial.keys()
if __name__ == "__main__":
appsvr() |
Partager