classification
Title: Let SimpleXMLRPCServer pass client_address to called functions.
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.2
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: acooke, cloverprince, draghuram, samwyse, terry.reedy, vstinner
Priority: normal Keywords:

Created on 2008-06-07 16:26 by cloverprince, last changed 2010-07-12 05:17 by terry.reedy. This issue is now closed.

Files
File name Uploaded Description Edit
AddressRevealingXMLRPCServer.py cloverprince, 2008-06-07 16:26
unnamed samwyse, 2010-07-12 05:10
Messages (9)
msg67811 - (view) Author: Kunshan Wang (cloverprince) Date: 2008-06-07 16:26
I recently wrote a program making use of SimpleXMLRPCServer.
It has a function that responds to the caller according to the caller's
IP address.  However the current SimpleXMLRPCServer does not allow me to
do this (directly).

For example:

def whoami():
    # blah blah blah ....
    return the_caller_s_ip_address
svr.register_function(whoami)

The problem is that only SimpleXMLRPCRequestHandler knows the client's
IP address.  I googled and searched the google group.  Many people
recommends subclassing the SimpleXMLRPCRequestHandler.  I did this, but
found that it is not very easy, although possible.

I managed to make the RequestHandler pass its member client_address to
function _dispatch by subclassing SimpleXMLRPCRequestHandler,
copy-and-paste the code from the library, and then add an extra argument
to _dispatch.  Now _dispatch function looks like this:

_dispatch(self, method, params, client_address)

By default _dispatch assumes that the first parameter of 'method' is
client_address, and then followed by other parameters from the (remote)
caller.  So if the XMLRPC client calls:

s=xmlrpclib.Server("http://localhost:9999")
s.whoami()

the server actually calls whoami(client_address), where whoami is
defined as:

def whoami(client_address):
    return client_address


The attachment contains a subclassed version of SimpleXMLRPCServer,
named AddressRevealingXMLRPCServer.  The code is ugly (because most
codes are copied, and is vulnerable to future library changes), but it
just works now.

However this leads to more problems:  Now that client_address can be
passed to the called function, it may be needed that more informations
could be passed to the called function, such as the HTTP headers, the
response time, etc..  

My code may not be the best solution.
I suggest there may be some mechanism (such as overridable methods) to
be added to SimpleXMLRPCServer so that user can specify *what* to pass
to the called function.
msg87888 - (view) Author: Samwyse (samwyse) * Date: 2009-05-16 12:43
A more general solution would be to pass the RequestHandler instance as
a parameter to the dispatch function.  This would allow the function to
pick out more than just the client address.  To avoid breaking
pre-existing code, this should be made optional, perhaps by adding a
keyword to the register_function method.  Something like this:

  def __init__(...):
    self._include_request = set()
  def register_function(..., include_request=False):
    self._include_request.add(method)

Later, the dispatch function would be invoked like this:
  kwds = {}
  if method in self._include_request:
    kwds.update(request=self)
  [...]
  return self.instance._dispatch(method, params, **kwds)
  [...]
  return func(client_address, *params, **kwds)
msg92016 - (view) Author: andrew cooke (acooke) Date: 2009-08-27 21:13
Came here wondering how best to solve this myself.

I already subclass the request handler to do client validation (password
etc) and it stuck me that a simpler solution would be to use thread
local storage.

This avoids having to modify dispatch.  The disadvantage is that this
uses a single global scope.  I don't think it's a better solution than
that suggested by samwyse for a final solution, but I thought I'd note
it here because it's a simpler alternative for people needing to add a
work-around, and I suspect many people aren't aware of the possibility.

>>> import _threading_local ; help(_threading_local)

If this wouldn't work (maybe I've misunderstood the server threading?)
then I'd appreciate someone correcting me.  Thanks.
msg98687 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2010-02-01 20:46
client_address is not interresting, but I prefer the handler object (SimpleXMLRPCRequestHandler instance). The handler contains more information:
 * handler.address_string() gives the FQDN
 * handler.connection.getpeername() gives the client address
 * handler.connection.getsockanem() gives the server address, useful is the server is listening to multiple addresses
 * etc.

If the server uses SSL, it would be possible to get more information about SSL from handler(.connection).

Your patch breaks the API for all (service) callbacks. Add an option to register_function() would allow a smoother transition (don't break API): you don't have to inherit from a new class. Eg. server.register_function(whoami, with_handler=True).
msg98688 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2010-02-01 20:47
Oops, I mean "client_address is interresting, but ..."
msg98689 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2010-02-01 20:52
Oops oops oops, samwyse wrote exactly the same idea than me in the first comment!
msg109679 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2010-07-09 04:33
In 3.x, class  SimpleXMLRPCServer lives in module xmlrpc.server
msg110066 - (view) Author: Samwyse (samwyse) * Date: 2010-07-12 05:10
More importantly, the dispatch method is now part of
the SimpleXMLRPCDispatcher, which (as a mix-in class) has no direct access
to the RequestHandler instance that comprises the request.  This breaks
Victor's and my idea, unless one is willing to subclass
SimpleXMLRPCRequestHandler to provide a_dispatch method.  The ability to do
this, however, is only provided for backward compatibility and I doubt it
could be part of a general solution.

On Thu, Jul 8, 2010 at 11:33 PM, Terry J. Reedy <report@bugs.python.org>wrote:

>
> Terry J. Reedy <tjreedy@udel.edu> added the comment:
>
> In 3.x, class  SimpleXMLRPCServer lives in module xmlrpc.server
>
> ----------
> nosy: +tjreedy
> versions: +Python 3.2 -Python 2.5
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue3058>
> _______________________________________
>
msg110067 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2010-07-12 05:17
I am taking your comment as a suggestion to close this.
History
Date User Action Args
2010-07-12 05:17:19terry.reedysetstatus: open -> closed
resolution: out of date
messages: + msg110067
2010-07-12 05:10:50samwysesetfiles: + unnamed

messages: + msg110066
2010-07-09 04:33:23terry.reedysetnosy: + terry.reedy

messages: + msg109679
versions: + Python 3.2, - Python 2.5
2010-02-01 20:52:14vstinnersetmessages: + msg98689
2010-02-01 20:47:21vstinnersetmessages: + msg98688
2010-02-01 20:46:27vstinnersetnosy: + vstinner
messages: + msg98687
2009-08-27 21:14:00acookesetnosy: + acooke
messages: + msg92016
2009-05-16 12:43:11samwysesetnosy: + samwyse
messages: + msg87888
2008-06-09 10:59:37draghuramsetnosy: + draghuram
2008-06-07 16:26:10cloverprincecreate