I'm writing a simple xmlrpc programe in python. something like the following:
def foo(data): # I want get the calling client's IP address here... How can I ? serv开发者_C百科er=SimpleXMLRPCServer.SimpleXMLRPCServer((host, port)) server.register_function(foo) server.handle_request()
As can be seen in the above, I want to get the client IP address in the registed function "foo", how can I ?
You may do so by subclassing the server (and possibly the handler, too). E.g.:
class MyXMLRPCServer(SimpleXMLRPCServer.SimpleXMLRPCServer):
def process_request(self, request, client_address):
self.client_address = client_address
return SimpleXMLRPCServer.SimpleXMLRPCServer.process_request(
self, request, client_address)
server=SimpleXMLRPCServer.MyXMLRPCServer((host, port))
Now server.client_address gives you the desired data. Note that this direct, short coding only works for the single-threaded case (which you're using anyway by choosing the simple server in your code) -- the need to work with the handler comes in if you want to go multi-threaded.
精彩评论