Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在继承自SimpleXMLRPCServer的类的uuuu init uuu上调用永远服务u_Python_Python 2.7_Simplexmlrpcserver - Fatal编程技术网

Python 在继承自SimpleXMLRPCServer的类的uuuu init uuu上调用永远服务u

Python 在继承自SimpleXMLRPCServer的类的uuuu init uuu上调用永远服务u,python,python-2.7,simplexmlrpcserver,Python,Python 2.7,Simplexmlrpcserver,这是错的吗 from SimpleXMLRPCServer import SimpleXMLRPCServer from random import randint def TicTacServer(SimpleXMLRPCServer): def __init__(self,host): super(TicTacServer,self).__init__(host) self.resetGame() super(TicTacServe

这是错的吗

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
以下是ProxyServer的代码:

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
回溯(最近一次呼叫最后一次):
文件“tictacClient.py”,第77行,在
client=tictaclient('http://localhost:8081')
文件“tictacClient.py”,第7行,在__
self.board=self.getBoard()
文件“C:\Python27\lib\xmlrpclib.py”,第1224行,在调用中__
返回self.\u发送(self.\u名称,args)
文件“C:\Python27\lib\xmlrpclib.py”,第1578行,在请求中
verbose=self.\uuu verbose
请求中第1264行的文件“C:\Python27\lib\xmlrpclib.py”
返回self.single\u请求(主机、处理程序、请求体、详细)
文件“C:\Python27\lib\xmlrpclib.py”,第1292行,在单次请求中
自我发送内容(h,请求正文)
文件“C:\Python27\lib\xmlrpclib.py”,第1439行,位于发送内容中
connection.endheaders(请求\正文)
文件“C:\Python27\lib\httplib.py”,第954行,在endheaders中
自发送输出(消息体)
文件“C:\Python27\lib\httplib.py”,第814行,在发送输出中
self.send(msg)
文件“C:\Python27\lib\httplib.py”,第776行,在send中
self.connect()
文件“C:\Python27\lib\httplib.py”,第757行,在connect中
self.timeout,self.source\u地址)
文件“C:\Python27\lib\socket.py”,第571行,位于create_connection中
提出错误
socket.error:[Errno 10061]无法建立连接,因为目标计算机主动拒绝了它

在检查了我找到的SimpleXMLRPCServer源代码后,witch正在工作,我现在可以连接到它了

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.

源代码告诉我,您只能执行
def\u listMethods(self):返回['addPlayer','getBoard',…]
,而不是向
super(TicTacServer,self)注册。注册函数(self.addPlayer)
在您的评论之后,我继续检查xmlrpc源文件,并找到了如何执行所需的操作。我将发布我自己问题的答案。