Package logilab :: Package common :: Module xmlrpcutils
[frames] | no frames]

Source Code for Module logilab.common.xmlrpcutils

  1  # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. 
  2  # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr 
  3  # 
  4  # This file is part of logilab-common. 
  5  # 
  6  # logilab-common is free software: you can redistribute it and/or modify it under 
  7  # the terms of the GNU Lesser General Public License as published by the Free 
  8  # Software Foundation, either version 2.1 of the License, or (at your option) any 
  9  # later version. 
 10  # 
 11  # logilab-common is distributed in the hope that it will be useful, but WITHOUT 
 12  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 13  # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more 
 14  # details. 
 15  # 
 16  # You should have received a copy of the GNU Lesser General Public License along 
 17  # with logilab-common.  If not, see <http://www.gnu.org/licenses/>. 
 18  """XML-RPC utilities. 
 19   
 20   
 21   
 22   
 23  """ 
 24  __docformat__ = "restructuredtext en" 
 25   
 26  import xmlrpclib 
 27  from base64 import encodestring 
 28  #from cStringIO import StringIO 
 29   
 30  ProtocolError = xmlrpclib.ProtocolError 
 31   
 32  ## class BasicAuthTransport(xmlrpclib.Transport): 
 33  ##     def __init__(self, username=None, password=None): 
 34  ##         self.username = username 
 35  ##         self.password = password 
 36  ##         self.verbose = None 
 37  ##         self.has_ssl = httplib.__dict__.has_key("HTTPConnection") 
 38   
 39  ##     def request(self, host, handler, request_body, verbose=None): 
 40  ##         # issue XML-RPC request 
 41  ##         if self.has_ssl: 
 42  ##             if host.startswith("https:"): h = httplib.HTTPSConnection(host) 
 43  ##             else: h = httplib.HTTPConnection(host) 
 44  ##         else: h = httplib.HTTP(host) 
 45   
 46  ##         h.putrequest("POST", handler) 
 47   
 48  ##         # required by HTTP/1.1 
 49  ##         if not self.has_ssl: # HTTPConnection already does 1.1 
 50  ##             h.putheader("Host", host) 
 51  ##         h.putheader("Connection", "close") 
 52   
 53  ##         if request_body: h.send(request_body) 
 54  ##         if self.has_ssl: 
 55  ##             response = h.getresponse() 
 56  ##             if response.status != 200: 
 57  ##                 raise xmlrpclib.ProtocolError(host + handler, 
 58  ##                                               response.status, 
 59  ##                                               response.reason, 
 60  ##                                               response.msg) 
 61  ##             file = response.fp 
 62  ##         else: 
 63  ##             errcode, errmsg, headers = h.getreply() 
 64  ##             if errcode != 200: 
 65  ##                 raise xmlrpclib.ProtocolError(host + handler, errcode, 
 66  ##                                               errmsg, headers) 
 67   
 68  ##             file = h.getfile() 
 69   
 70  ##         return self.parse_response(file) 
 71   
 72   
 73   
74 -class AuthMixin:
75 """basic http authentication mixin for xmlrpc transports""" 76
77 - def __init__(self, username, password, encoding):
78 self.verbose = 0 79 self.username = username 80 self.password = password 81 self.encoding = encoding
82
83 - def request(self, host, handler, request_body, verbose=0):
84 """issue XML-RPC request""" 85 h = self.make_connection(host) 86 h.putrequest("POST", handler) 87 # required by XML-RPC 88 h.putheader("User-Agent", self.user_agent) 89 h.putheader("Content-Type", "text/xml") 90 h.putheader("Content-Length", str(len(request_body))) 91 h.putheader("Host", host) 92 h.putheader("Connection", "close") 93 # basic auth 94 if self.username is not None and self.password is not None: 95 h.putheader("AUTHORIZATION", "Basic %s" % encodestring( 96 "%s:%s" % (self.username, self.password)).replace("\012", "")) 97 h.endheaders() 98 # send body 99 if request_body: 100 h.send(request_body) 101 # get and check reply 102 errcode, errmsg, headers = h.getreply() 103 if errcode != 200: 104 raise ProtocolError(host + handler, errcode, errmsg, headers) 105 file = h.getfile() 106 ## # FIXME: encoding ??? iirc, this fix a bug in xmlrpclib but... 107 ## data = h.getfile().read() 108 ## if self.encoding != 'UTF-8': 109 ## data = data.replace("version='1.0'", 110 ## "version='1.0' encoding='%s'" % self.encoding) 111 ## result = StringIO() 112 ## result.write(data) 113 ## result.seek(0) 114 ## return self.parse_response(result) 115 return self.parse_response(file)
116
117 -class BasicAuthTransport(AuthMixin, xmlrpclib.Transport):
118 """basic http authentication transport"""
119
120 -class BasicAuthSafeTransport(AuthMixin, xmlrpclib.SafeTransport):
121 """basic https authentication transport"""
122 123
124 -def connect(url, user=None, passwd=None, encoding='ISO-8859-1'):
125 """return an xml rpc server on <url>, using user / password if specified 126 """ 127 if user or passwd: 128 assert user and passwd is not None 129 if url.startswith('https://'): 130 transport = BasicAuthSafeTransport(user, passwd, encoding) 131 else: 132 transport = BasicAuthTransport(user, passwd, encoding) 133 else: 134 transport = None 135 server = xmlrpclib.ServerProxy(url, transport, encoding=encoding) 136 return server
137