pythonpython-2.7simplexmlrpcserver

Calling serve_forever on __init__ of a class that inherits from SimpleXMLRPCServer


Is this wrong?

from SimpleXMLRPCServer import SimpleXMLRPCServer
from random import randint

def TicTacServer(SimpleXMLRPCServer):

    def __init__(self,host):
        super(TicTacServer,self).__init__(host)
        self.resetGame()
        super(TicTacServer,self).register_function(self.addPlayer)
        super(TicTacServer,self).register_function(self.getBoard)
        super(TicTacServer,self).register_function(self.whoGoesFirst)
        super(TicTacServer,self).register_function(self.insertMove)
        super(TicTacServer,self).register_function(self.whosTurnIsIt)
        super(TicTacServer,self).register_function(self.gameIsReady)
        super(TicTacServer,self).register_function(self.resetGame)
        super(TicTacServer,self).serve_forever()

All those functions are declared and work as intended. I don't know if this is possible, python doesn't throw any errors but i can't connect to it using

xmlrpclib.ServerProxy

Here is the code for the ProxyServer:

from xmlrpclib import ServerProxy

class TicTacClient(ServerProxy):
    def __init__(self,host):
        ServerProxy.__init__(self,host)
        self.board = self.getBoard()

and here is the error i get

Traceback (most recent call last):
  File "tictacClient.py", line 77, in <module>
    client = TicTacClient('http://localhost:8081')
  File "tictacClient.py", line 7, in __init__
    self.board = self.getBoard()
  File "C:\Python27\lib\xmlrpclib.py", line 1224, in __call__
    return self.__send(self.__name, args)
  File "C:\Python27\lib\xmlrpclib.py", line 1578, in __request
    verbose=self.__verbose
  File "C:\Python27\lib\xmlrpclib.py", line 1264, in request
    return self.single_request(host, handler, request_body, verbose)
  File "C:\Python27\lib\xmlrpclib.py", line 1292, in single_request
    self.send_content(h, request_body)
  File "C:\Python27\lib\xmlrpclib.py", line 1439, in send_content
    connection.endheaders(request_body)
  File "C:\Python27\lib\httplib.py", line 954, in endheaders
    self._send_output(message_body)
  File "C:\Python27\lib\httplib.py", line 814, in _send_output
    self.send(msg)
  File "C:\Python27\lib\httplib.py", line 776, in send
    self.connect()
  File "C:\Python27\lib\httplib.py", line 757, in connect
    self.timeout, self.source_address)
  File "C:\Python27\lib\socket.py", line 571, in create_connection
    raise err
socket.error: [Errno 10061] No connection could be made because the target machine actively refused it

Solution

  • After checking the SimpleXMLRPCServer source i came up with this, witch is working and i can now connect to it.

    from SimpleXMLRPCServer import SimpleXMLRPCServer
    
    class TicTacServer(SimpleXMLRPCServer):
    
        board = [u' '] * 10
        player = []
    
        public = ('addPlayer','getBoard','whoGoesFirst','insertMove','whosTurnIsIt','gameIsReady','resetGame')
    
        def _dispatch(self, method, params):
    
            if method in self.public:
                func = getattr(self,method)
                return func(*params)
            else:
                raise Exception('method "%s" is not supported' % method)
    
    
    
    server = TicTacServer(('localhost',8081))
    server.serve_forever()
    

    From Python27/Lib/SimpleXMLRPCServer.py:

    The default implementation
    attempts to dispatch XML-RPC calls to the functions or instance
    installed in the server. Override the _dispatch method inhereted
    from SimpleXMLRPCDispatcher to change this behavior.