0

这是错的吗?

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()

所有这些功能都已声明并按预期工作。我不知道这是否可能,python 不会抛出任何错误,但我无法使用它连接到它

xmlrpclib.ServerProxy

这是代理服务器的代码:

from xmlrpclib import ServerProxy

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

这是我得到的错误

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
4

1 回答 1

1

在检查了 SimpleXMLRPCServer 源之后,我想出了这个,女巫正在工作,我现在可以连接到它。

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()

来自 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.
于 2013-04-04T12:22:10.113 回答