SimpleXMLRPCServer

Brian Quinlan brian at sweetapp.com
Mon Jan 7 18:22:41 CET 2002


Lewis Bergman wrote:
> After much headbanging (and some heIp from the list) I came up with
this
> method to register functions from multiple classes. As per the docs
the
> server can only natively instantiate one class at a time and that was
less
> than I needed. I don't know if anyone else would ever need it but here
it
> is anyway. This is my first program in python so it probably isn't
worth
> looking at.

Why can't you simply write something like:

class MyMethods:
	def __init__(self, nixUser, icradiusUser):
		self.NixUser = nixUser
		self.icradiusUser = icradiusUser

NixUser = NixUser.NixUser()
icradiusUser = icradiusUser.icradiusUser()

server = SimpleXMLRPCServer(("localhost", 8000))
server.register_instance(MyMethods(NixUser, icradiusUser))
server.serve_forever()

I haven't tested this so don't be shocked if there you need to fiddle
with it to make it work. Let me know if it doesn't do what you want it
to do.

Also, masking module names with variable names is probably a bad idea.

Cheers,
Brian

> Here is the whole thing so you can get an idea.
> 
> #!/usr/local/bin/python2.2
> from SimpleXMLRPCServer import *
> import re
> import NixUser, icradiusUser
> 
> NixUser = NixUser.NixUser()
> icradiusUser = icradiusUser.icradiusUser()
> 
> server = SimpleXMLRPCServer(("localhost", 8000))
> 
> importedClass = [NixUser, icradiusUser]
> for name in importedClass:
> 	for method in dir(name):
> 		if re.match("^[_]", method) is None:
> 			funcName = name.__module__ + '.' + method
> 			server.register_function(getattr(name, method),
> funcName)
> 
> server.serve_forever()





More information about the Python-list mailing list