XML-RPC support for the repoze.bfg web framework.
repoze.bfg.xmlrpc is a package that ships outside the main repoze.bfg distribution. To install the package, use easy_install:
easy_install -i http://dist.repoze.org/bfgsite/simple repoze.bfg.xmlrpc
Or obtain the packge via http://svn.repoze.org/repoze.bfg.xmlrpc and use setup.py install.
Create a function in the form below. The function will be meant to be called with positional parameters from an XML-RPC request.
1 2 | def say_hello(context, name):
return 'Hello, %s' % name
|
Then add the @xmlrpc_view decorator to the function.
1 2 3 4 5 | from repoze.bfg.xmlrpc import xmlrpc_view
@xmlrpc_view
def say_hello(context, name):
return 'Hello, %s' % name
|
Then configure your application registry to point to the say_hello view.
1 2 3 4 5 | <bfg:view
name="say_hello"
view=".views.say_hello"
for="*"
/>
|
Then call the function via an XML-RPC client. Note that any XML-RPC methodName will be ignored; you must point the client directly at the view URL; traversal doesn’t work from there.
1 2 3 4 | >>> from xmlrpclib import ServerProxy
>>> s = ServerProxy('http://localhost:6543/say_hello')
>>> s('Chris')
Hello, Chris
|
Function meant to be used as a decorator. The xmlrpc_view function turns functions which accept params and return Python structures into functions suitable for use as bfg views which an XML-RPC response. The decorated function must accept a context argument and zero or more positional arguments (conventionally named *params).
E.g.:
from repoze.bfg.xmlrpc import xmlrpc_view
@xmlrpc_view
def say(context, what):
if what == 'hello'
return {'say':'Hello!'}
else:
return {'say':'Goodbye!'}
Equates to:
from repoze.bfg.xmlrpc import parse_xmlrpc_request
from repoze.bfg.xmlrpc import xmlrpc_response
def say_view(context, request):
params, method = parse_xmlrpc_request(request)
return say(context, *params)
def say(context, what):
if what == 'hello'
return {'say':'Hello!'}
else:
return {'say':'Goodbye!'}
Note that if you use repoze.bfg.convention, you must decorate your view function in the following order for it to be recognized by the convention machinery as a view:
@bfg_view(name='say')
@xmlrpc_view
def say(context, what):
if what == 'hello'
return {'say':'Hello!'}
else:
return {'say':'Goodbye!'}
In other words do not decorate it in xmlrpc_view, then bfg_view order; it won’t work.