You are not logged in Log in Join
You are here: Home » Members » mcdonc » Scripts » XMLRPCBasicAuth.py

Log in
Name

Password

 

XMLRPCBasicAuth.py

File details
Size
1 K
File type
text/x-python

File contents

import sys

# change this line to point to somewhere where xmlrpclib can be
# imported from!
sys.path.insert(0, '/home/chrism/sandboxes/2_3Branch/lib/python')
import xmlrpclib
import base64
import string

def Server(url, username, password):
    t = BasicAuthTransport(username, password)
    return xmlrpclib.Server(url, t)

class BasicAuthTransport(xmlrpclib.Transport):
    """Handles an HTTP transaction to an XML-RPC server with basicauth """
    
    def __init__(self, username, password):
        self.username = username
        self.password = password

    def request(self, host, handler, request_body):
        # issue XML-RPC request

        username = self.username
        password = self.password
        
        import httplib
        h = httplib.HTTP(host)
        h.putrequest("POST", handler)

        # required by HTTP/1.1
        h.putheader("Host", host)

        # required by XML-RPC
        h.putheader("User-Agent", self.user_agent)
        h.putheader("Content-Type", "text/xml")
        h.putheader("Content-Length", str(len(request_body)))

        # basic auth
        if username and password:
            h.putheader("AUTHORIZATION", "Basic %s" % string.replace(
                base64.encodestring("%s:%s" % (username, password)),
                "\012", ""))

        h.endheaders()
        
        if request_body:
            h.send(request_body)

        errcode, errmsg, headers = h.getreply()

        if errcode != 200:
            raise ProtocolError(
                host + handler,
                errcode, errmsg,
                headers
                )

        return self.parse_response(h.getfile())


def test():
    s = Server('http://serenade.digicool.com:8083/', 'user1', 'mypass')
    print s.testmethod()

if __name__ == '__main__':
    test()