Back to index

moin  1.9.0~rc2
BasicAuthTransport.py
Go to the documentation of this file.
00001 # taken from Amos' XML-RPC HowTo:
00002 
00003 import xmlrpclib, httplib
00004 from base64 import encodestring
00005 
00006 class BasicAuthTransport(xmlrpclib.Transport):
00007     def __init__(self, username=None, password=None):
00008         self.username = username
00009         self.password = password
00010         self.verbose = 0
00011 
00012     def request(self, host, handler, request_body, **kw):
00013         # issue XML-RPC request
00014         h = httplib.HTTP(host)
00015         h.putrequest("POST", handler)
00016 
00017         # required by HTTP/1.1
00018         h.putheader("Host", host)
00019 
00020         # required by XML-RPC
00021         h.putheader("User-Agent", self.user_agent)
00022         h.putheader("Content-Type", "text/xml")
00023         h.putheader("Content-Length", str(len(request_body)))
00024 
00025         # basic auth
00026         if self.username is not None and self.password is not None:
00027             authhdr = "Basic %s" % encodestring("%s:%s" % (self.username, self.password)).replace("\012", "")
00028             h.putheader("Authorization", authhdr)
00029         h.endheaders()
00030 
00031         if request_body:
00032             h.send(request_body)
00033 
00034         errcode, errmsg, headers = h.getreply()
00035 
00036         if errcode != 200:
00037             raise xmlrpclib.ProtocolError(
00038                 host + handler,
00039                 errcode, errmsg,
00040                 headers
00041                 )
00042 
00043         return self.parse_response(h.getfile())
00044