views:

123

answers:

1

I create little SimpleXMLRPCServer for check ip of client.

I try this:

Server

import xmlrpclib

from SimpleXMLRPCServer import SimpleXMLRPCServer

server = SimpleXMLRPCServer(("localhost", 8000))

def MyIp(): return "Your ip is: %s" % server.socket.getpeername()

server.register_function(MyIp)

server.serve_forever()

Client

import xmlrpclib

se = xmlrpclib.Server("http://localhost:8000")

print se.MyIp()

Error

xmlrpclib.Fault: :(107, 'Transport endpoint is not connected')">

How make client_address visible to all functions?

+2  A: 

If you want for example to pass client_address as the first argument to every function, you could subclass SimpleXMLRPCRequestHandler (pass your subclass as the handler when you instantiate SimpleXMLRPCServer) and override _dispatch (to prepend self.client_address to the params tuple and then delegate the rest to SimpleXMLRPCRequestHandler._dispatch). If this approach is OK and you want to see code, just ask!

I'm not sure how you'd safely use anything but the function arguments to "make client_address visible" -- there's no client_address as a bare name, global or otherwise, there's just the self.client_address of each instance of the request handler class (and hacks such as copying it to a global variables feel really yucky indeed -- and unsafe under threading, etc etc).

Alex Martelli
I am using the same approach. Subclass of SimpleXMLRPCRequestHandler and appending client_address to parameters of calling function.
Jiri