Authentication for XML-RPC Calls

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • whitemice

    Authentication for XML-RPC Calls

    The only documentation regarding doing authentication for XML-RPC I
    can find is -

    "Both the HTTP and HTTPS transports support the URL syntax extension
    for HTTP Basic Authentication: http://user:pass@host:port/path. The
    user:pass portion will be base64-encoded as an HTTP `Authorization'
    header, and sent to the remote server as part of the connection
    process when invoking an XML-RPC method. You only need to use this if
    the remote server requires a Basic Authentication user and password."

    - from http://docs.python.org/lib/module-xmlrpclib.html

    Is this really the only way to do authentication for XML-RPC calls?

    Like this:
    server = xmlrpclib.Serve r('http://adam:fred123@lo calhost/zidestore/so/
    adam/'

    This works, but is pretty ugly. Is there no way to setup the
    authentication through properties (like in most XML-RPC bindings),
    like:

    server = xmlrpclib.Serve r('http://localhost/zidestore/so/adam/'
    server.Username = 'adam'
    server.Password = 'fred123'
  • Larry Bates

    #2
    Re: Authentication for XML-RPC Calls

    whitemice wrote:
    The only documentation regarding doing authentication for XML-RPC I
    can find is -
    >
    "Both the HTTP and HTTPS transports support the URL syntax extension
    for HTTP Basic Authentication: http://user:pass@host:port/path. The
    user:pass portion will be base64-encoded as an HTTP `Authorization'
    header, and sent to the remote server as part of the connection
    process when invoking an XML-RPC method. You only need to use this if
    the remote server requires a Basic Authentication user and password."
    >
    - from http://docs.python.org/lib/module-xmlrpclib.html
    >
    Is this really the only way to do authentication for XML-RPC calls?
    >
    Like this:
    server = xmlrpclib.Serve r('http://adam:fred123@lo calhost/zidestore/so/
    adam/'
    >
    This works, but is pretty ugly. Is there no way to setup the
    authentication through properties (like in most XML-RPC bindings),
    like:
    >
    server = xmlrpclib.Serve r('http://localhost/zidestore/so/adam/'
    server.Username = 'adam'
    server.Password = 'fred123'
    Just write a little factory class that does it for you:

    import urlparse

    class myauth(object):
    def __init__(self, scheme = None, domain = None, path = None):
    self.scheme = scheme
    self.domain = domain
    self.path = path
    self.Username = None
    self.Password = None

    def __str__(self):

    for attr in ['scheme', 'domain', 'path', 'Username', 'Password']:
    if getattr(self, attr) is None:
    raise ValueError('No %s attribute value given' % attr)

    url=urlparse.ur lunsplit((self. scheme,
    '%s:%s@%s' % (self.Username, self.Password, self.domain),
    self.path, '', ''))

    return url

    if __name__ == "__main__":
    auth = auth = myauth(scheme = 'http', domain = 'localhost',
    path='/zidestore/so/adam/')

    auth.Username = 'adam'
    auth.Password = 'fred123'
    print auth


    In program

    auth = myauth(scheme = 'http', domain = 'localhost',
    path = '/zidestore/so/adam')

    auth.Username = 'adam'
    auth.Password = 'fred123'

    print auth

    'http://adam:fred123@do cs.python.org/params;/lib/module-xmlrpclib.html'
    >>>
    -Larry

    Comment

    Working...