return datetime.datetime.now() def show_type(self, arg): """Illustrates how types are passed in and out of server methods. Accepts one argument of any type. Returns a tuple with string representation of the value, the name of the type, and the value itself. """ return (str(arg), str(type(arg)), arg) def raises_exception(self, msg): "Always raises a RuntimeError with the message passed in" raise RuntimeError(msg) def send_back_binary(self, bin): """Accepts single Binary argument, and unpacks and repacks it to return it.""" data = bin.data response = Binary(data) return response server.register_instance(ExampleService()) try: print 'Use Control-C to exit' server.serve_forever() except KeyboardInterrupt: print 'Exiting'
from network_programming.SimpleXMLRPCServer import SimpleXMLRPCServer, list_public_methods server = SimpleXMLRPCServer(("localhost", 9000), logRequests=True) server.register_introspection_functions() class DirectoryService: def _listMethods(self): return list_public_methods(self) def _methodHelp(self, method): f = getattr(self, method) return inspect.getdoc(f) def list(self, dir_name): """list(dir_name) => [<filenames>] Returns a list containing the contents of the named directory. """ return os.listdir(dir_name) server.register_instance(DirectoryService()) try: print "Use Control-C to exit" server.serve_forever() except KeyboardInterrupt: print "Exiting"
# Copyright (c) 2008 Doug Hellmann All rights reserved. # """ """ __version__ = "$Id$" #end_pymotw_header import inspect import os from network_programming.SimpleXMLRPCServer import SimpleXMLRPCServer server = SimpleXMLRPCServer(('localhost', 9000), logRequests=True) class ServiceRoot: pass class DirectoryService: def list(self, dir_name): return os.listdir(dir_name) root = ServiceRoot() root.dir = DirectoryService() server.register_instance(root, allow_dotted_names=True) try: print 'Use Control-C to exit' server.serve_forever() except KeyboardInterrupt: print 'Exiting'
return getattr(f, 'exposed', False) class MyService: PREFIX = 'prefix' def _dispatch(self, method, params): # Remove our prefix from the method name if not method.startswith(self.PREFIX + '.'): raise Exception('method "%s" is not supported' % method) method_name = method.partition('.')[2] func = getattr(self, method_name) if not is_exposed(func): raise Exception('method "%s" is not supported' % method) return func(*params) @expose def public(self): return 'This is public' def private(self): return 'This is private' server.register_instance(MyService()) try: print 'Use Control-C to exit' server.serve_forever() except KeyboardInterrupt: print 'Exiting'