The page contents below were copied with permission of author (BrianDorsey) from [http://www.seapig.org/DocXMLRPCServer DocXMLRPCServer]. I'd like to rework the page to fit the form of WorkingWithTime, RssLibraries, etc.,. See also: XmlRpc -- LionKimbro DateTime(2004-04-28T05:58:27Z)
Here is a simple server which implements the above message procedure as well as a wait procedure. This server is based on the DocXMLRPCServer included with Python2.3; In Python2.2, use SimpleXMLRPCServer instead, and don't call server.register_introspection_functions().
1 # ExampleServer.py 2 import time 3 import socket 4 from DocXMLRPCServer import DocXMLRPCServer 5 6 class SimpleShareServer: 7 def message(self, msg): 8 """message('Print me!') => True 9 10 Log everything passed to this function""" 11 print time.asctime(), msg 12 return True 13 14 def wait(self, seconds): 15 """wait(5) => 5 16 17 Wait for a certain number of seconds before returning. 18 Returns the same number passed in.""" 19 print time.asctime(), "Waiting %s seconds" % seconds 20 time.sleep(seconds) 21 print time.asctime(), "Finished waiting %s seconds" % seconds 22 return seconds 23 24 25 if __name__ == '__main__': 26 server = DocXMLRPCServer(("", 8000), logRequests=0) 27 server.register_introspection_functions() 28 server.register_instance(SimpleShareServer()) 29 30 print time.asctime(), 'Application Starting.' 31 server.serve_forever() 32 print time.asctime(), 'Application Finishing.'
The benefit of using DocXMLRPCServer is that it automatically creates documentation for your XML-RPC server, just open a browser and head to http://localhost:8000 after starting the server.
Writing a client to call the wait function is left as an exercise for the reader.
XmlRpc -- general information on XML-RPC
BaseHttpServer -- class that DocXmlRpcServer inherits from
[http://www.python.org/doc/current/lib/module-SimpleXMLRPCServer.html SimpleXMLRPCServer documentation]
[http://www.python.org/doc/current/lib/module-DocXMLRPCServer.html DocXMLRPCServer documentation]
SimpleXMLRPCServer.py, and DocXMLRPCServer.py - comments includ many examples of use
[http://sourceforge.net/projects/py-xmlrpc/ Fast C implementation of XML-RPC for python] (only for python version up to 2.2) [http://www.xmlrpc.com/discuss/msgReader$1573 (more notes on it- apparently 20-100x faster!)]
I'd like a Python script where:
You stick the script in a directory and run it to start a DocXmlRpcServer.
command line options (OptParse) for host, port,...
- Scans all modules in the directory for "XMLRPC_namespace='eggs'"
- Publishes the functions in the modules that name their XML-RPC namespace.
- Functions that start with an underscore, don't get published.
This way, you can just write:
..., and, given the script, you have everything you need to make your service callable remotely.
I'll probably end up making this myself, and posting it here. It's not terribly complicated. The only part I don't know well is the ModulesAsPlugins part-- automatically detecting modules, loading them, and looping through them.