2016-08-17 2 views
0

xmlrpcを使ってPythonでクライアントとサーバーを構築しようとしていますが、メソッドを持つFunctionWrapperクラスを使用しなければなりません。 sendMessage_wrapper(self、message)、サーバーは別のクラスで宣言されていますが、サーバーにメソッドを登録しようとしていますが、クライアントからメソッドを呼び出すときに発生します。Pythonでxmlrpcを使ってサーバーとクライアントを構築する

Cliente:

#! /usr/bin/env python 
# -*- coding: utf-8 -*- 
import sys 
import xmlrpclib 
from SimpleXMLRPCServer import SimpleXMLRPCServer 

from os import path 
sys.path.append(path.dirname(path.dirname(path.abspath(__file__)))) 
from Constants.Constants import * 

class MyApiClient: 
    def __init__(self, contact_port = DEFAULT_PORT,contact_ip=LOCALHOST_CLIENT): 
     self.contact_port = contact_port 
     self.contact_ip = contact_ip 
     self.proxy = xmlrpclib.ServerProxy(contact_ip+str(self.contact_port)+"/") 


    def sendMessage(self,message): 
     self.proxy.sendMessage_wrapper(message) 

a = MyApiClient() 
a.sendMessage("Hi") 
a.sendMessage("WORKING") 

サーバー:

#! /usr/bin/env python 
# -*- coding: utf-8 -*- 
import sys 
import xmlrpclib 
from SimpleXMLRPCServer import SimpleXMLRPCServer 

from os import path 
sys.path.append(path.dirname(path.dirname(path.abspath(__file__)))) 
from Constants.Constants import * 

class MyApiServer: 
    def __init__(self,wrapper, my_port = DEFAULT_PORT): 
     self.port = my_port 
     self.server = SimpleXMLRPCServer((LOCALHOST,self.port)) 
     self.wrapper = wrapper 
     self.server.register_instance(self.wrapper) 
     print("Running") 
     self.server.serve_forever() 


class FunctionWrapper: 
    def __init__(self): 
     self.message = None 

    """ 
    Procedimiento que ofrece nuestro servidor, este metodo sera llamado 
    por el cliente con el que estamos hablando, debe de 
    hacer lo necesario para mostrar el texto en nuestra pantalla. 
    """ 
    def sendMessage_wrapper(self, message): 
     self.message = message 
     self.showMessage() 
    def showMessage(self): 
     print ("Mensaje "+self.message) 
     #raise NotImplementedError("Should have implemented this") 


a = FunctionWrapper() 
b = MyApiServer(a) 

答えて

0

ここでは、あなたがそれを

#! /usr/bin/env python 
# -*- coding: utf-8 -*- 

#Nombres para etiquetas login local y remoto 
MY_PORT_NUMBER_TITLE = "Cual es mi puerto?" 
OTHER_PORT_NUMBER_TITLE = "Cual es el puerto de contacto?" 
OTHER_IP_NUMBER_TITLE = "Cual es la direccion ip de contacto?" 
LOGIN_TITLE = "Acceder" 

#Nombres para las etiquetas del chat 
CONVERSATION_TITLE = "Conversacion" 
SEND_TITLE = "Responder" 

#Titulo de las ventans GUI 
LOGIN_WINDOW = "Login" 
CHAT_WINDOW = "Chat" 

#Modos de acceso al chat, local o remoto 
LOCAL = "Local" 
REMOTE = "Remote" 

#Mensajes de error 
WARNING = "¡Alerta!" 
MISSING_MESSAGE = "No hay ningun mensaje para enviar" 

#Localhost 
LOCALHOST = "localhost" 
DEFAULT_PORT = 5000 
LOCALHOST_CLIENT = "http://localhost:" 
+0

を必要とする場合の定数は、私はクライアント self.proxy = xmlrpclibで使用してそれを解決しています。 ServerProxy(contact_ip + str(self.contact_port)+ "/"、allow_none = True) これはサーバーで: self.server = SimpleXMLRPCServer((LOCALHOST、self.port)、allow_none = True) –

関連する問題