By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,958 Members | 1,139 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,958 IT Pros & Developers. It's quick & easy.

XML-RPC "filter"

P: n/a
Dear all,

I'm writing an XML-RPC server which should be able to modify the
incoming request before dispatching it. In particular I wand to added
two fixed parameters to the method called: one is the client host
address, and the other is the user name provided as for Basic
Authentication (http://us**@www.bla-bla.com).

To do this, at the present I've overwritten the do_POST method of
SimpleXMLRPCRequestHandler, including at a certain point this code:

.....
data = ''.join(L)

params, method = xmlrpclib.loads(data)
user = "unknown"
if self.headers.has_key('Authorization'):
# handle Basic authentication
(enctype, encstr) = self.headers.get('Authorization').split()
user, password = base64.standard_b64decode(encstr).split(':')
params = list(params)
params.append(self.address_string())
params.append(user)
params = tuple(params)
data = xmlrpclib.dumps(params, methodname=method)

(I slightly modified it to make it more readable at mail level)

It works, but I don't really like it because it completely overwrites
the do_POST method that in the future Python releases is going to
change (I verified it). Do you know a better way to do this?

Thanks in advance.

Luigi
Sep 9 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Luigi wrote:
Dear all,

I'm writing an XML-RPC server which should be able to modify the
incoming request before dispatching it. In particular I wand to added
two fixed parameters to the method called: one is the client host
address, and the other is the user name provided as for Basic
Authentication (http://us**@www.bla-bla.com).

To do this, at the present I've overwritten the do_POST method of
SimpleXMLRPCRequestHandler, including at a certain point this code:

....
data = ''.join(L)

params, method = xmlrpclib.loads(data)
user = "unknown"
if self.headers.has_key('Authorization'):
# handle Basic authentication
(enctype, encstr) = self.headers.get('Authorization').split()
user, password = base64.standard_b64decode(encstr).split(':')
params = list(params)
params.append(self.address_string())
params.append(user)
params = tuple(params)
data = xmlrpclib.dumps(params, methodname=method)

(I slightly modified it to make it more readable at mail level)

It works, but I don't really like it because it completely overwrites
the do_POST method that in the future Python releases is going to
change (I verified it). Do you know a better way to do this?
I would go for a slightly different approach: make your server have a
dispatch-method that delegates the calls to the underlying actual
implementation. But *before* that happens, extract the information as
above, and either

- prepend it to the argument list

- stuff it into threadlocal variables, and only access these if needed in
your implementation.

Diez
Sep 9 '08 #2

P: n/a
On 9 Set, 17:55, "Diez B. Roggisch" <de...@nospam.web.dewrote:
I would go for a slightly different approach: make your server have a
dispatch-method that delegates the calls to the underlying actual
implementation. But *before* that happens, extract the information as
above, and either

*- prepend it to the argument list

*- stuff it into threadlocal variables, and only access these if neededin
your implementation.

Diez
Are you suggesting me to overwrite the _dispatch(self, method, params)
method of SimpleXMLRPCDispatcher? I thought to this possibility, but
it only accepts "method" and "params" as arguments, so, as far as I
know, I have no way to get the user and host address to append.

Perhaps I've misunderstood your suggestion... in that case can you
post a short example?

Thank you very much!

Luigi
Sep 10 '08 #3

P: n/a
On Sep 9, 8:53*am, Luigi <luigipai...@libero.itwrote:
Dear all,

I'm writing an XML-RPC server which should be able to modify the
incoming request before dispatching it. In particular I wand to added
two fixed parameters to the method called: one is the client host
address, and the other is the user name provided as for Basic
Authentication (http://u...@www.bla-bla.com).

To do this, at the present I've overwritten the do_POST method of
SimpleXMLRPCRequestHandler, including at a certain point this code:

....
data = ''.join(L)

params, method = xmlrpclib.loads(data)
user = "unknown"
if self.headers.has_key('Authorization'):
* # handle Basic authentication
* (enctype, encstr) = *self.headers.get('Authorization').split()
* user, password = base64.standard_b64decode(encstr).split(':')
params = list(params)
params.append(self.address_string())
params.append(user)
params = tuple(params)
data = xmlrpclib.dumps(params, methodname=method)

(I slightly modified it to make it more readable at mail level)

It works, but I don't really like it because it completely overwrites
the do_POST method that in the future Python releases is going to
change (I verified it). Do you know a better way to do this?

Thanks in advance.

Luigi
I actually wrote a wsgi module for almost this -exact- use case
(having to prepend a user/password to the method calls). The simple
rpc server and dispatchers didn't give me enough control over the
behavior, so I had to reimplement all the logic surround the loads/
dumps calls, and eventually that just turned into the bulk of the
whole SimpleXMLRPCServer module. There's a lot of tight coupling in
the _dispatch method, so you'll have to override, monkey patch, or
reimplement it.
Sep 10 '08 #4

P: n/a
lu**********@gmail.com schrieb:
On 9 Set, 17:55, "Diez B. Roggisch" <de...@nospam.web.dewrote:
>I would go for a slightly different approach: make your server have a
dispatch-method that delegates the calls to the underlying actual
implementation. But *before* that happens, extract the information as
above, and either

- prepend it to the argument list

- stuff it into threadlocal variables, and only access these if needed in
your implementation.

Diez

Are you suggesting me to overwrite the _dispatch(self, method, params)
method of SimpleXMLRPCDispatcher? I thought to this possibility, but
it only accepts "method" and "params" as arguments, so, as far as I
know, I have no way to get the user and host address to append.

Perhaps I've misunderstood your suggestion... in that case can you
post a short example?
Ah, darn. Yes, you are right of course, the information itself is not
available, as you don't have access to the request. I gotta ponder this
a bit more.

Diez
Sep 10 '08 #5

P: n/a
On Sep 10, 2:04*pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
luigi.pai...@gmail.com schrieb:
On 9 Set, 17:55, "Diez B. Roggisch" <de...@nospam.web.dewrote:
I would go for a slightly different approach: make your server have a
dispatch-method that delegates the calls to the underlying actual
implementation. But *before* that happens, extract the information as
above, and either
*- prepend it to the argument list
*- stuff it into threadlocal variables, and only access these if needed in
your implementation.
Diez
Are you suggesting me to overwrite the _dispatch(self, method, params)
method of SimpleXMLRPCDispatcher? I thought to this possibility, but
it only accepts "method" and "params" as arguments, so, as far as I
know, I have no way to get the user and host address to append.
Perhaps I've misunderstood your suggestion... in that case can you
post a short example?

Ah, darn. Yes, you are right of course, the information itself is not
available, as you don't have access to the request. I gotta ponder this
a bit more.

Diez
Because he wants to insert parameters at the very start, he can
probably get away with modifying the xml directly. Just find the
position of the <params(i think thats the tag) and insert the xml
you need after it. Its pretty dirty, but would work. The wire format
isn't that complicated.
Sep 11 '08 #6

P: n/a
On 11 Set, 18:45, Richard Levasseur <richard...@gmail.comwrote:
Because he wants to insert parameters at the very start, he can
probably get away with modifying the xml directly. *Just find the
position of the <params(i think thats the tag) and insert the xml
you need after it. *Its pretty dirty, but would work. *The wire format
isn't that complicated.
I think this is exactly what I do... isn't it?
Sep 12 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.