Search code examples
pythonhttpserversimplexmlrpcserver

Combine SimpleXMLRPCServer and BaseHTTPRequestHandler in Python


Because cross-domain xmlrpc requests are not possible in JavaScript I need to create a Python app which exposes both some HTML through HTTP and an XML-RPC service on the same domain.

Creating an HTTP request handler and SimpleXMLRPCServer in python is quite easy, but they both have to listen on a different port, which means a different domain.

Is there a way to create something that will listen on a single port on the localhost and expose both the HTTPRequestHandler and XMLRPCRequest handler?

Right now I have two different services:

httpServer = HTTPServer(('localhost',8001), HttpHandler);
xmlRpcServer = SimpleXMLRPCServer(('localhost',8000),requestHandler=RequestHandler)

Update

  • I cannot install Apache on the device
  • The hosted page will be a single html page
  • The only client will be the device on witch the python service runs itself

Solution

  • The solution was actually quite simple, based on Wai Yip Tung's reply:

    All I had to do was keep using the SimpleXMLRPCServer instance, but modify the handler:

    class RequestHandler(SimpleXMLRPCRequestHandler):
        rpc_paths = ('/RPC2',)
    
        def do_GET(self):
              #implementation here
    

    This will cause the handler to respond to GET requests as well as the original POST (XML-RPC) requests.