Exception XML-RPC class (Python)

Asked

Viewed 46 times

0

I’m trying to create an XML-RPC class, but it’s giving an Exception, but I don’t understand why. It’s just a simple example:

server.py

from xmlrpc.server import SimpleXMLRPCServer
import pickle

class Animal:
 def __init__(self, number_of_paws, color):
   self.number_of_paws = number_of_paws
   self.color = color

class Sheep(Animal):
 def __init__(self, color):
   Animal.__init__(self, 4, color)     

def main():
  print("This is a server!")
  server = SimpleXMLRPCServer(("localhost", 8080))
  server.register_function(Animal.__init__)
  server.register_function(Sheep.__init__)
  server.serve_forever()

if __name__ == "__main__":
  main()

client py.

import xmlrpc.client
import time
import pickle
import server

def main():
 print("This is a client!")
 client = xmlrpc.client.ServerProxy("http://localhost:8080")
 mary = client.server.Sheep("white")
 my_pickled_mary = pickle.dumps(mary)
 dolly = pickle.loads(my_pickled_mary)
 dolly.color = "black"
 print (str.format("Dolly is {0} ", dolly.color))
 print (str.format("Mary is {0} ", mary.color))

if __name__ == "__main__":
 start = time.time()
 main()
 tempo_total = ("--- %s seconds ---" % (time.time() - start))
 print("Tempo Total: " + str(tempo_total))

The server runs normal, but when I run the client, this following error occurs:

xmlrpc.client.Fault: :method "server. Sheep" is not supported'>

Which means the method is not supported?

1 answer

1


Server register_function should look like

  server.register_function(Animal, "Animal")
  server.register_function(Sheep, "Sheep")

And in the Customer change the line 8 to be as

  with xmlrpc.client.ServerProxy("http://localhost:8080") as client:

And for a simpler exeplo of how it works test only with

  mary = client.Sheep("white")
  my_pickled_mary = pickle.dumps(mary)
  print (str.format("Mary is {0} ", mary))

The Exit was:

Mary is {'number_of_paws': 4, 'color': 'white'} 
Tempo Total: --- 0.0032961368560791016 seconds ---

Browser other questions tagged

You are not signed in. Login or sign up in order to post.