Authentication for XML-RPC Calls

Larry Bates larry.bates at websafe.com`
Tue Jul 22 14:16:15 EDT 2008


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.Server('http://adam:fred123@localhost/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.Server('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.urlunsplit((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@docs.python.org/params;/lib/module-xmlrpclib.html'
 >>>

-Larry



More information about the Python-list mailing list