Combining SimpleXMLRPCServer and BaseHTTPRequestHandler in Python

Since cross-domain xmlrpc requests are not possible in JavaScript I need to create a Python application that provides both HTML and HTTP and an XML-RPC service in the same domain.

Creating an HTTP request handler and SimpleXMLRPCServer in python is pretty simple, but they should both 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 local host and set the HTTPRequestHandler and XMLRPCRequest handler?

Now I have two different services:

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

Update

  • I can not install Apache on the device
  • There is one html page per page
  • The only client will be the device running the python service
+3
3

, :

, , SimpleXMLRPCServer, :

class RequestHandler(SimpleXMLRPCRequestHandler):
    rpc_paths = ('/RPC2',)

    def do_GET(self):
          #implementation here

GET, POST (XML-RPC).

+2

SocketServer.TCPServer. - , .

HTTPServer -XML-RPC SimpleXMLRPCServer.

+2

HTTPServer - . -, Apache, Python CGI ( , mod_wsgi).

Then the web server runs on a single port, and you can directly process the HTML code on the web server and write as many CGI scripts as you like in Python, as an example for XMLRPC requests using CGIXMLRPCRequestHandler.

class MyFuncs:
    def div(self, x, y) : return x // y


handler = CGIXMLRPCRequestHandler()
handler.register_function(pow)
handler.register_function(lambda x,y: x+y, 'add')
handler.register_introspection_functions()
handler.register_instance(MyFuncs())
handler.handle_request()
0
source

Source: https://habr.com/ru/post/1761404/


All Articles