Вызов serve_forever для __init__ класса, который наследуется от SimpleXMLRPCServer
Это неправильно?
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
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.