SimpleRPCServer

E

Esben Pedersen

I am trying to restrict some of the methods available by RPC to whether
the user is connected through a ssh-tunnel eg. connecting from localhost.

class UserRPC:
'''Methods available to users'''
pass

class AdminRPC(UserRPC):
'''Methods available only when connecting from localhost'''
pass

class RPCServer(SimpleXMLRPCServer):
def _dispatch(self, method, args):
if user_is_connecting_from_localhost():
return getattr(AdminRPC(), method)(*args)
return getattr(UserRPC(), method)(*args)

What i don't know is how to the client ip. I would think I should
override the _marshalled_dispatch method but I can't quite grasp it all.

/Esben
 
S

Skip Montanaro

Esben> What i don't know is how to the client ip. I would think I should
Esben> override the _marshalled_dispatch method but I can't quite grasp
Esben> it all.

First, from my reading of SimpleXMLRPCServer, I don't think _dispatch()
belongs at that level. It belongs in the request handler class or in a
separate dispatcher class, depending on what version of Python you're using.
Second, SimpleXMLRPCServer is a SocketServer subclass. You should be able
to provide a verify_request method (see the SocketServer docs) and a handler
class with a set_local method:

class RPCServer(SimpleXMLRPCServer):
def verify_request(self, handler, address):
handler.set_local(address_is_local(address))

A _dispatch method in the handler will then be able to check an attribute to
know if the request was made from the local machine or not.

Skip
 
R

robin

Skip Montanaro said:
First, from my reading of SimpleXMLRPCServer, I don't think _dispatch()
belongs at that level. It belongs in the request handler class or in a
separate dispatcher class, depending on what version of Python you're using.

Quite so. As a variant I just use verify_request() to persist the
client IP address, and then wait until _dispatch() to do everything
else.

class RPCServer(SimpleXMLRPCServer):
def verify_request(self, handler, address):
self.client_ip, self.client_port = address
return True

def _dispatch(self, method, args):
do_something(self.client_ip)

Though using a firewall would not be remiss. :)

-- robin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top