]> granicus.if.org Git - python/commitdiff
Issue #19082: Working xmlrpc.server and xmlrpc.client examples. Both in modules and...
authorSenthil Kumaran <senthil@uthcode.com>
Mon, 13 Jan 2014 00:06:58 +0000 (16:06 -0800)
committerSenthil Kumaran <senthil@uthcode.com>
Mon, 13 Jan 2014 00:06:58 +0000 (16:06 -0800)
Doc/library/xmlrpc.server.rst
Lib/xmlrpc/client.py
Lib/xmlrpc/server.py
Misc/NEWS

index 18fee2fc1d365cffc028c815d1d5bbf459ca68bf..aca4f366bb21158e303744bf4748030dbd7c072f 100644 (file)
@@ -184,6 +184,70 @@ server::
    # Print list of available methods
    print(s.system.listMethods())
 
+The following example included in `Lib/xmlrpc/server.py` module shows a server
+allowing dotted names and registering a multicall function.
+
+.. warning::
+
+  Enabling the *allow_dotted_names* option allows intruders to access your
+  module's global variables and may allow intruders to execute arbitrary code on
+  your machine.  Only use this example only within a secure, closed network.
+
+::
+
+    import datetime
+
+    class ExampleService:
+        def getData(self):
+            return '42'
+
+        class currentTime:
+            @staticmethod
+            def getCurrentTime():
+                return datetime.datetime.now()
+
+    server = SimpleXMLRPCServer(("localhost", 8000))
+    server.register_function(pow)
+    server.register_function(lambda x,y: x+y, 'add')
+    server.register_instance(ExampleService(), allow_dotted_names=True)
+    server.register_multicall_functions()
+    print('Serving XML-RPC on localhost port 8000')
+    try:
+        server.serve_forever()
+    except KeyboardInterrupt:
+        print("\nKeyboard interrupt received, exiting.")
+        server.server_close()
+        sys.exit(0)
+
+This ExampleService demo can be invoked from the command line::
+
+    python -m xmlrpc.server
+
+
+The client that interacts with the above server is included in
+`Lib/xmlrpc/client.py`::
+
+    server = ServerProxy("http://localhost:8000")
+
+    try:
+        print(server.currentTime.getCurrentTime())
+    except Error as v:
+        print("ERROR", v)
+
+    multi = MultiCall(server)
+    multi.getData()
+    multi.pow(2,9)
+    multi.add(1,2)
+    try:
+        for response in multi():
+            print(response)
+    except Error as v:
+        print("ERROR", v)
+
+This client which interacts with the demo XMLRPC server can be invoked as::
+
+    python -m xmlrpc.client
+
 
 CGIXMLRPCRequestHandler
 -----------------------
index 0f9522aa3cb64fd7c37a74c97d888f124d28b263..461c3005ffbae7cb0afacc6da1a12a3db2e29fa9 100644 (file)
@@ -1460,18 +1460,18 @@ if __name__ == "__main__":
 
     # simple test program (from the XML-RPC specification)
 
-    # server = ServerProxy("http://localhost:8000") # local server
-    server = ServerProxy("http://time.xmlrpc.com/RPC2")
+    # local server, available from Lib/xmlrpc/server.py
+    server = ServerProxy("http://localhost:8000")
 
     try:
         print(server.currentTime.getCurrentTime())
     except Error as v:
         print("ERROR", v)
 
-    # The server at xmlrpc.com doesn't seem to support multicall anymore.
     multi = MultiCall(server)
-    multi.currentTime.getCurrentTime()
-    multi.currentTime.getCurrentTime()
+    multi.getData()
+    multi.pow(2,9)
+    multi.add(1,2)
     try:
         for response in multi():
             print(response)
index 78ca4e0a4afcae46dbf49de9751892c8d11538f6..d27bf5a177e95a45d1269e8640e1d8575040f0d6 100644 (file)
@@ -967,10 +967,24 @@ class DocCGIXMLRPCRequestHandler(   CGIXMLRPCRequestHandler,
 
 
 if __name__ == '__main__':
+    import datetime
+
+    class ExampleService:
+        def getData(self):
+            return '42'
+
+        class currentTime:
+            @staticmethod
+            def getCurrentTime():
+                return datetime.datetime.now()
+
     server = SimpleXMLRPCServer(("localhost", 8000))
     server.register_function(pow)
     server.register_function(lambda x,y: x+y, 'add')
+    server.register_instance(ExampleService(), allow_dotted_names=True)
+    server.register_multicall_functions()
     print('Serving XML-RPC on localhost port 8000')
+    print('It is advisable to run this example server within a secure, closed network.')
     try:
         server.serve_forever()
     except KeyboardInterrupt:
index 6439b2ddd46e229b1a364f087138bba0cf8a9117..196eb23f4d332b2d6201eb3615d608c6b4f8fc08 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -43,6 +43,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #19082: Working xmlrpc.server and xmlrpc.client examples. Both in
+  modules and in documentation. Initial patch contributed by Vajrasky Kok.
+
 - Issue #20138: The wsgiref.application_uri() and wsgiref.request_uri()
   functions now conform to PEP 3333 when handle non-ASCII URLs.