jpayne@68: # jpayne@68: # XML-RPC CLIENT LIBRARY jpayne@68: # $Id$ jpayne@68: # jpayne@68: # an XML-RPC client interface for Python. jpayne@68: # jpayne@68: # the marshalling and response parser code can also be used to jpayne@68: # implement XML-RPC servers. jpayne@68: # jpayne@68: # Notes: jpayne@68: # this version is designed to work with Python 2.1 or newer. jpayne@68: # jpayne@68: # History: jpayne@68: # 1999-01-14 fl Created jpayne@68: # 1999-01-15 fl Changed dateTime to use localtime jpayne@68: # 1999-01-16 fl Added Binary/base64 element, default to RPC2 service jpayne@68: # 1999-01-19 fl Fixed array data element (from Skip Montanaro) jpayne@68: # 1999-01-21 fl Fixed dateTime constructor, etc. jpayne@68: # 1999-02-02 fl Added fault handling, handle empty sequences, etc. jpayne@68: # 1999-02-10 fl Fixed problem with empty responses (from Skip Montanaro) jpayne@68: # 1999-06-20 fl Speed improvements, pluggable parsers/transports (0.9.8) jpayne@68: # 2000-11-28 fl Changed boolean to check the truth value of its argument jpayne@68: # 2001-02-24 fl Added encoding/Unicode/SafeTransport patches jpayne@68: # 2001-02-26 fl Added compare support to wrappers (0.9.9/1.0b1) jpayne@68: # 2001-03-28 fl Make sure response tuple is a singleton jpayne@68: # 2001-03-29 fl Don't require empty params element (from Nicholas Riley) jpayne@68: # 2001-06-10 fl Folded in _xmlrpclib accelerator support (1.0b2) jpayne@68: # 2001-08-20 fl Base xmlrpclib.Error on built-in Exception (from Paul Prescod) jpayne@68: # 2001-09-03 fl Allow Transport subclass to override getparser jpayne@68: # 2001-09-10 fl Lazy import of urllib, cgi, xmllib (20x import speedup) jpayne@68: # 2001-10-01 fl Remove containers from memo cache when done with them jpayne@68: # 2001-10-01 fl Use faster escape method (80% dumps speedup) jpayne@68: # 2001-10-02 fl More dumps microtuning jpayne@68: # 2001-10-04 fl Make sure import expat gets a parser (from Guido van Rossum) jpayne@68: # 2001-10-10 sm Allow long ints to be passed as ints if they don't overflow jpayne@68: # 2001-10-17 sm Test for int and long overflow (allows use on 64-bit systems) jpayne@68: # 2001-11-12 fl Use repr() to marshal doubles (from Paul Felix) jpayne@68: # 2002-03-17 fl Avoid buffered read when possible (from James Rucker) jpayne@68: # 2002-04-07 fl Added pythondoc comments jpayne@68: # 2002-04-16 fl Added __str__ methods to datetime/binary wrappers jpayne@68: # 2002-05-15 fl Added error constants (from Andrew Kuchling) jpayne@68: # 2002-06-27 fl Merged with Python CVS version jpayne@68: # 2002-10-22 fl Added basic authentication (based on code from Phillip Eby) jpayne@68: # 2003-01-22 sm Add support for the bool type jpayne@68: # 2003-02-27 gvr Remove apply calls jpayne@68: # 2003-04-24 sm Use cStringIO if available jpayne@68: # 2003-04-25 ak Add support for nil jpayne@68: # 2003-06-15 gn Add support for time.struct_time jpayne@68: # 2003-07-12 gp Correct marshalling of Faults jpayne@68: # 2003-10-31 mvl Add multicall support jpayne@68: # 2004-08-20 mvl Bump minimum supported Python version to 2.1 jpayne@68: # 2014-12-02 ch/doko Add workaround for gzip bomb vulnerability jpayne@68: # jpayne@68: # Copyright (c) 1999-2002 by Secret Labs AB. jpayne@68: # Copyright (c) 1999-2002 by Fredrik Lundh. jpayne@68: # jpayne@68: # info@pythonware.com jpayne@68: # http://www.pythonware.com jpayne@68: # jpayne@68: # -------------------------------------------------------------------- jpayne@68: # The XML-RPC client interface is jpayne@68: # jpayne@68: # Copyright (c) 1999-2002 by Secret Labs AB jpayne@68: # Copyright (c) 1999-2002 by Fredrik Lundh jpayne@68: # jpayne@68: # By obtaining, using, and/or copying this software and/or its jpayne@68: # associated documentation, you agree that you have read, understood, jpayne@68: # and will comply with the following terms and conditions: jpayne@68: # jpayne@68: # Permission to use, copy, modify, and distribute this software and jpayne@68: # its associated documentation for any purpose and without fee is jpayne@68: # hereby granted, provided that the above copyright notice appears in jpayne@68: # all copies, and that both that copyright notice and this permission jpayne@68: # notice appear in supporting documentation, and that the name of jpayne@68: # Secret Labs AB or the author not be used in advertising or publicity jpayne@68: # pertaining to distribution of the software without specific, written jpayne@68: # prior permission. jpayne@68: # jpayne@68: # SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD jpayne@68: # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT- jpayne@68: # ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR jpayne@68: # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY jpayne@68: # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, jpayne@68: # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS jpayne@68: # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE jpayne@68: # OF THIS SOFTWARE. jpayne@68: # -------------------------------------------------------------------- jpayne@68: jpayne@68: """ jpayne@68: An XML-RPC client interface for Python. jpayne@68: jpayne@68: The marshalling and response parser code can also be used to jpayne@68: implement XML-RPC servers. jpayne@68: jpayne@68: Exported exceptions: jpayne@68: jpayne@68: Error Base class for client errors jpayne@68: ProtocolError Indicates an HTTP protocol error jpayne@68: ResponseError Indicates a broken response package jpayne@68: Fault Indicates an XML-RPC fault package jpayne@68: jpayne@68: Exported classes: jpayne@68: jpayne@68: ServerProxy Represents a logical connection to an XML-RPC server jpayne@68: jpayne@68: MultiCall Executor of boxcared xmlrpc requests jpayne@68: DateTime dateTime wrapper for an ISO 8601 string or time tuple or jpayne@68: localtime integer value to generate a "dateTime.iso8601" jpayne@68: XML-RPC value jpayne@68: Binary binary data wrapper jpayne@68: jpayne@68: Marshaller Generate an XML-RPC params chunk from a Python data structure jpayne@68: Unmarshaller Unmarshal an XML-RPC response from incoming XML event message jpayne@68: Transport Handles an HTTP transaction to an XML-RPC server jpayne@68: SafeTransport Handles an HTTPS transaction to an XML-RPC server jpayne@68: jpayne@68: Exported constants: jpayne@68: jpayne@68: (none) jpayne@68: jpayne@68: Exported functions: jpayne@68: jpayne@68: getparser Create instance of the fastest available parser & attach jpayne@68: to an unmarshalling object jpayne@68: dumps Convert an argument tuple or a Fault instance to an XML-RPC jpayne@68: request (or response, if the methodresponse option is used). jpayne@68: loads Convert an XML-RPC packet to unmarshalled data plus a method jpayne@68: name (None if not present). jpayne@68: """ jpayne@68: jpayne@68: import base64 jpayne@68: import sys jpayne@68: import time jpayne@68: from datetime import datetime jpayne@68: from decimal import Decimal jpayne@68: import http.client jpayne@68: import urllib.parse jpayne@68: from xml.parsers import expat jpayne@68: import errno jpayne@68: from io import BytesIO jpayne@68: try: jpayne@68: import gzip jpayne@68: except ImportError: jpayne@68: gzip = None #python can be built without zlib/gzip support jpayne@68: jpayne@68: # -------------------------------------------------------------------- jpayne@68: # Internal stuff jpayne@68: jpayne@68: def escape(s): jpayne@68: s = s.replace("&", "&") jpayne@68: s = s.replace("<", "<") jpayne@68: return s.replace(">", ">",) jpayne@68: jpayne@68: # used in User-Agent header sent jpayne@68: __version__ = '%d.%d' % sys.version_info[:2] jpayne@68: jpayne@68: # xmlrpc integer limits jpayne@68: MAXINT = 2**31-1 jpayne@68: MININT = -2**31 jpayne@68: jpayne@68: # -------------------------------------------------------------------- jpayne@68: # Error constants (from Dan Libby's specification at jpayne@68: # http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php) jpayne@68: jpayne@68: # Ranges of errors jpayne@68: PARSE_ERROR = -32700 jpayne@68: SERVER_ERROR = -32600 jpayne@68: APPLICATION_ERROR = -32500 jpayne@68: SYSTEM_ERROR = -32400 jpayne@68: TRANSPORT_ERROR = -32300 jpayne@68: jpayne@68: # Specific errors jpayne@68: NOT_WELLFORMED_ERROR = -32700 jpayne@68: UNSUPPORTED_ENCODING = -32701 jpayne@68: INVALID_ENCODING_CHAR = -32702 jpayne@68: INVALID_XMLRPC = -32600 jpayne@68: METHOD_NOT_FOUND = -32601 jpayne@68: INVALID_METHOD_PARAMS = -32602 jpayne@68: INTERNAL_ERROR = -32603 jpayne@68: jpayne@68: # -------------------------------------------------------------------- jpayne@68: # Exceptions jpayne@68: jpayne@68: ## jpayne@68: # Base class for all kinds of client-side errors. jpayne@68: jpayne@68: class Error(Exception): jpayne@68: """Base class for client errors.""" jpayne@68: __str__ = object.__str__ jpayne@68: jpayne@68: ## jpayne@68: # Indicates an HTTP-level protocol error. This is raised by the HTTP jpayne@68: # transport layer, if the server returns an error code other than 200 jpayne@68: # (OK). jpayne@68: # jpayne@68: # @param url The target URL. jpayne@68: # @param errcode The HTTP error code. jpayne@68: # @param errmsg The HTTP error message. jpayne@68: # @param headers The HTTP header dictionary. jpayne@68: jpayne@68: class ProtocolError(Error): jpayne@68: """Indicates an HTTP protocol error.""" jpayne@68: def __init__(self, url, errcode, errmsg, headers): jpayne@68: Error.__init__(self) jpayne@68: self.url = url jpayne@68: self.errcode = errcode jpayne@68: self.errmsg = errmsg jpayne@68: self.headers = headers jpayne@68: def __repr__(self): jpayne@68: return ( jpayne@68: "<%s for %s: %s %s>" % jpayne@68: (self.__class__.__name__, self.url, self.errcode, self.errmsg) jpayne@68: ) jpayne@68: jpayne@68: ## jpayne@68: # Indicates a broken XML-RPC response package. This exception is jpayne@68: # raised by the unmarshalling layer, if the XML-RPC response is jpayne@68: # malformed. jpayne@68: jpayne@68: class ResponseError(Error): jpayne@68: """Indicates a broken response package.""" jpayne@68: pass jpayne@68: jpayne@68: ## jpayne@68: # Indicates an XML-RPC fault response package. This exception is jpayne@68: # raised by the unmarshalling layer, if the XML-RPC response contains jpayne@68: # a fault string. This exception can also be used as a class, to jpayne@68: # generate a fault XML-RPC message. jpayne@68: # jpayne@68: # @param faultCode The XML-RPC fault code. jpayne@68: # @param faultString The XML-RPC fault string. jpayne@68: jpayne@68: class Fault(Error): jpayne@68: """Indicates an XML-RPC fault package.""" jpayne@68: def __init__(self, faultCode, faultString, **extra): jpayne@68: Error.__init__(self) jpayne@68: self.faultCode = faultCode jpayne@68: self.faultString = faultString jpayne@68: def __repr__(self): jpayne@68: return "<%s %s: %r>" % (self.__class__.__name__, jpayne@68: self.faultCode, self.faultString) jpayne@68: jpayne@68: # -------------------------------------------------------------------- jpayne@68: # Special values jpayne@68: jpayne@68: ## jpayne@68: # Backwards compatibility jpayne@68: jpayne@68: boolean = Boolean = bool jpayne@68: jpayne@68: ## jpayne@68: # Wrapper for XML-RPC DateTime values. This converts a time value to jpayne@68: # the format used by XML-RPC. jpayne@68: #

jpayne@68: # The value can be given as a datetime object, as a string in the jpayne@68: # format "yyyymmddThh:mm:ss", as a 9-item time tuple (as returned by jpayne@68: # time.localtime()), or an integer value (as returned by time.time()). jpayne@68: # The wrapper uses time.localtime() to convert an integer to a time jpayne@68: # tuple. jpayne@68: # jpayne@68: # @param value The time, given as a datetime object, an ISO 8601 string, jpayne@68: # a time tuple, or an integer time value. jpayne@68: jpayne@68: jpayne@68: # Issue #13305: different format codes across platforms jpayne@68: _day0 = datetime(1, 1, 1) jpayne@68: if _day0.strftime('%Y') == '0001': # Mac OS X jpayne@68: def _iso8601_format(value): jpayne@68: return value.strftime("%Y%m%dT%H:%M:%S") jpayne@68: elif _day0.strftime('%4Y') == '0001': # Linux jpayne@68: def _iso8601_format(value): jpayne@68: return value.strftime("%4Y%m%dT%H:%M:%S") jpayne@68: else: jpayne@68: def _iso8601_format(value): jpayne@68: return value.strftime("%Y%m%dT%H:%M:%S").zfill(17) jpayne@68: del _day0 jpayne@68: jpayne@68: jpayne@68: def _strftime(value): jpayne@68: if isinstance(value, datetime): jpayne@68: return _iso8601_format(value) jpayne@68: jpayne@68: if not isinstance(value, (tuple, time.struct_time)): jpayne@68: if value == 0: jpayne@68: value = time.time() jpayne@68: value = time.localtime(value) jpayne@68: jpayne@68: return "%04d%02d%02dT%02d:%02d:%02d" % value[:6] jpayne@68: jpayne@68: class DateTime: jpayne@68: """DateTime wrapper for an ISO 8601 string or time tuple or jpayne@68: localtime integer value to generate 'dateTime.iso8601' XML-RPC jpayne@68: value. jpayne@68: """ jpayne@68: jpayne@68: def __init__(self, value=0): jpayne@68: if isinstance(value, str): jpayne@68: self.value = value jpayne@68: else: jpayne@68: self.value = _strftime(value) jpayne@68: jpayne@68: def make_comparable(self, other): jpayne@68: if isinstance(other, DateTime): jpayne@68: s = self.value jpayne@68: o = other.value jpayne@68: elif isinstance(other, datetime): jpayne@68: s = self.value jpayne@68: o = _iso8601_format(other) jpayne@68: elif isinstance(other, str): jpayne@68: s = self.value jpayne@68: o = other jpayne@68: elif hasattr(other, "timetuple"): jpayne@68: s = self.timetuple() jpayne@68: o = other.timetuple() jpayne@68: else: jpayne@68: otype = (hasattr(other, "__class__") jpayne@68: and other.__class__.__name__ jpayne@68: or type(other)) jpayne@68: raise TypeError("Can't compare %s and %s" % jpayne@68: (self.__class__.__name__, otype)) jpayne@68: return s, o jpayne@68: jpayne@68: def __lt__(self, other): jpayne@68: s, o = self.make_comparable(other) jpayne@68: return s < o jpayne@68: jpayne@68: def __le__(self, other): jpayne@68: s, o = self.make_comparable(other) jpayne@68: return s <= o jpayne@68: jpayne@68: def __gt__(self, other): jpayne@68: s, o = self.make_comparable(other) jpayne@68: return s > o jpayne@68: jpayne@68: def __ge__(self, other): jpayne@68: s, o = self.make_comparable(other) jpayne@68: return s >= o jpayne@68: jpayne@68: def __eq__(self, other): jpayne@68: s, o = self.make_comparable(other) jpayne@68: return s == o jpayne@68: jpayne@68: def timetuple(self): jpayne@68: return time.strptime(self.value, "%Y%m%dT%H:%M:%S") jpayne@68: jpayne@68: ## jpayne@68: # Get date/time value. jpayne@68: # jpayne@68: # @return Date/time value, as an ISO 8601 string. jpayne@68: jpayne@68: def __str__(self): jpayne@68: return self.value jpayne@68: jpayne@68: def __repr__(self): jpayne@68: return "<%s %r at %#x>" % (self.__class__.__name__, self.value, id(self)) jpayne@68: jpayne@68: def decode(self, data): jpayne@68: self.value = str(data).strip() jpayne@68: jpayne@68: def encode(self, out): jpayne@68: out.write("") jpayne@68: out.write(self.value) jpayne@68: out.write("\n") jpayne@68: jpayne@68: def _datetime(data): jpayne@68: # decode xml element contents into a DateTime structure. jpayne@68: value = DateTime() jpayne@68: value.decode(data) jpayne@68: return value jpayne@68: jpayne@68: def _datetime_type(data): jpayne@68: return datetime.strptime(data, "%Y%m%dT%H:%M:%S") jpayne@68: jpayne@68: ## jpayne@68: # Wrapper for binary data. This can be used to transport any kind jpayne@68: # of binary data over XML-RPC, using BASE64 encoding. jpayne@68: # jpayne@68: # @param data An 8-bit string containing arbitrary data. jpayne@68: jpayne@68: class Binary: jpayne@68: """Wrapper for binary data.""" jpayne@68: jpayne@68: def __init__(self, data=None): jpayne@68: if data is None: jpayne@68: data = b"" jpayne@68: else: jpayne@68: if not isinstance(data, (bytes, bytearray)): jpayne@68: raise TypeError("expected bytes or bytearray, not %s" % jpayne@68: data.__class__.__name__) jpayne@68: data = bytes(data) # Make a copy of the bytes! jpayne@68: self.data = data jpayne@68: jpayne@68: ## jpayne@68: # Get buffer contents. jpayne@68: # jpayne@68: # @return Buffer contents, as an 8-bit string. jpayne@68: jpayne@68: def __str__(self): jpayne@68: return str(self.data, "latin-1") # XXX encoding?! jpayne@68: jpayne@68: def __eq__(self, other): jpayne@68: if isinstance(other, Binary): jpayne@68: other = other.data jpayne@68: return self.data == other jpayne@68: jpayne@68: def decode(self, data): jpayne@68: self.data = base64.decodebytes(data) jpayne@68: jpayne@68: def encode(self, out): jpayne@68: out.write("\n") jpayne@68: encoded = base64.encodebytes(self.data) jpayne@68: out.write(encoded.decode('ascii')) jpayne@68: out.write("\n") jpayne@68: jpayne@68: def _binary(data): jpayne@68: # decode xml element contents into a Binary structure jpayne@68: value = Binary() jpayne@68: value.decode(data) jpayne@68: return value jpayne@68: jpayne@68: WRAPPERS = (DateTime, Binary) jpayne@68: jpayne@68: # -------------------------------------------------------------------- jpayne@68: # XML parsers jpayne@68: jpayne@68: class ExpatParser: jpayne@68: # fast expat parser for Python 2.0 and later. jpayne@68: def __init__(self, target): jpayne@68: self._parser = parser = expat.ParserCreate(None, None) jpayne@68: self._target = target jpayne@68: parser.StartElementHandler = target.start jpayne@68: parser.EndElementHandler = target.end jpayne@68: parser.CharacterDataHandler = target.data jpayne@68: encoding = None jpayne@68: target.xml(encoding, None) jpayne@68: jpayne@68: def feed(self, data): jpayne@68: self._parser.Parse(data, 0) jpayne@68: jpayne@68: def close(self): jpayne@68: try: jpayne@68: parser = self._parser jpayne@68: except AttributeError: jpayne@68: pass jpayne@68: else: jpayne@68: del self._target, self._parser # get rid of circular references jpayne@68: parser.Parse(b"", True) # end of data jpayne@68: jpayne@68: # -------------------------------------------------------------------- jpayne@68: # XML-RPC marshalling and unmarshalling code jpayne@68: jpayne@68: ## jpayne@68: # XML-RPC marshaller. jpayne@68: # jpayne@68: # @param encoding Default encoding for 8-bit strings. The default jpayne@68: # value is None (interpreted as UTF-8). jpayne@68: # @see dumps jpayne@68: jpayne@68: class Marshaller: jpayne@68: """Generate an XML-RPC params chunk from a Python data structure. jpayne@68: jpayne@68: Create a Marshaller instance for each set of parameters, and use jpayne@68: the "dumps" method to convert your data (represented as a tuple) jpayne@68: to an XML-RPC params chunk. To write a fault response, pass a jpayne@68: Fault instance instead. You may prefer to use the "dumps" module jpayne@68: function for this purpose. jpayne@68: """ jpayne@68: jpayne@68: # by the way, if you don't understand what's going on in here, jpayne@68: # that's perfectly ok. jpayne@68: jpayne@68: def __init__(self, encoding=None, allow_none=False): jpayne@68: self.memo = {} jpayne@68: self.data = None jpayne@68: self.encoding = encoding jpayne@68: self.allow_none = allow_none jpayne@68: jpayne@68: dispatch = {} jpayne@68: jpayne@68: def dumps(self, values): jpayne@68: out = [] jpayne@68: write = out.append jpayne@68: dump = self.__dump jpayne@68: if isinstance(values, Fault): jpayne@68: # fault instance jpayne@68: write("\n") jpayne@68: dump({'faultCode': values.faultCode, jpayne@68: 'faultString': values.faultString}, jpayne@68: write) jpayne@68: write("\n") jpayne@68: else: jpayne@68: # parameter block jpayne@68: # FIXME: the xml-rpc specification allows us to leave out jpayne@68: # the entire block if there are no parameters. jpayne@68: # however, changing this may break older code (including jpayne@68: # old versions of xmlrpclib.py), so this is better left as jpayne@68: # is for now. See @XMLRPC3 for more information. /F jpayne@68: write("\n") jpayne@68: for v in values: jpayne@68: write("\n") jpayne@68: dump(v, write) jpayne@68: write("\n") jpayne@68: write("\n") jpayne@68: result = "".join(out) jpayne@68: return result jpayne@68: jpayne@68: def __dump(self, value, write): jpayne@68: try: jpayne@68: f = self.dispatch[type(value)] jpayne@68: except KeyError: jpayne@68: # check if this object can be marshalled as a structure jpayne@68: if not hasattr(value, '__dict__'): jpayne@68: raise TypeError("cannot marshal %s objects" % type(value)) jpayne@68: # check if this class is a sub-class of a basic type, jpayne@68: # because we don't know how to marshal these types jpayne@68: # (e.g. a string sub-class) jpayne@68: for type_ in type(value).__mro__: jpayne@68: if type_ in self.dispatch.keys(): jpayne@68: raise TypeError("cannot marshal %s objects" % type(value)) jpayne@68: # XXX(twouters): using "_arbitrary_instance" as key as a quick-fix jpayne@68: # for the p3yk merge, this should probably be fixed more neatly. jpayne@68: f = self.dispatch["_arbitrary_instance"] jpayne@68: f(self, value, write) jpayne@68: jpayne@68: def dump_nil (self, value, write): jpayne@68: if not self.allow_none: jpayne@68: raise TypeError("cannot marshal None unless allow_none is enabled") jpayne@68: write("") jpayne@68: dispatch[type(None)] = dump_nil jpayne@68: jpayne@68: def dump_bool(self, value, write): jpayne@68: write("") jpayne@68: write(value and "1" or "0") jpayne@68: write("\n") jpayne@68: dispatch[bool] = dump_bool jpayne@68: jpayne@68: def dump_long(self, value, write): jpayne@68: if value > MAXINT or value < MININT: jpayne@68: raise OverflowError("int exceeds XML-RPC limits") jpayne@68: write("") jpayne@68: write(str(int(value))) jpayne@68: write("\n") jpayne@68: dispatch[int] = dump_long jpayne@68: jpayne@68: # backward compatible jpayne@68: dump_int = dump_long jpayne@68: jpayne@68: def dump_double(self, value, write): jpayne@68: write("") jpayne@68: write(repr(value)) jpayne@68: write("\n") jpayne@68: dispatch[float] = dump_double jpayne@68: jpayne@68: def dump_unicode(self, value, write, escape=escape): jpayne@68: write("") jpayne@68: write(escape(value)) jpayne@68: write("\n") jpayne@68: dispatch[str] = dump_unicode jpayne@68: jpayne@68: def dump_bytes(self, value, write): jpayne@68: write("\n") jpayne@68: encoded = base64.encodebytes(value) jpayne@68: write(encoded.decode('ascii')) jpayne@68: write("\n") jpayne@68: dispatch[bytes] = dump_bytes jpayne@68: dispatch[bytearray] = dump_bytes jpayne@68: jpayne@68: def dump_array(self, value, write): jpayne@68: i = id(value) jpayne@68: if i in self.memo: jpayne@68: raise TypeError("cannot marshal recursive sequences") jpayne@68: self.memo[i] = None jpayne@68: dump = self.__dump jpayne@68: write("\n") jpayne@68: for v in value: jpayne@68: dump(v, write) jpayne@68: write("\n") jpayne@68: del self.memo[i] jpayne@68: dispatch[tuple] = dump_array jpayne@68: dispatch[list] = dump_array jpayne@68: jpayne@68: def dump_struct(self, value, write, escape=escape): jpayne@68: i = id(value) jpayne@68: if i in self.memo: jpayne@68: raise TypeError("cannot marshal recursive dictionaries") jpayne@68: self.memo[i] = None jpayne@68: dump = self.__dump jpayne@68: write("\n") jpayne@68: for k, v in value.items(): jpayne@68: write("\n") jpayne@68: if not isinstance(k, str): jpayne@68: raise TypeError("dictionary key must be string") jpayne@68: write("%s\n" % escape(k)) jpayne@68: dump(v, write) jpayne@68: write("\n") jpayne@68: write("\n") jpayne@68: del self.memo[i] jpayne@68: dispatch[dict] = dump_struct jpayne@68: jpayne@68: def dump_datetime(self, value, write): jpayne@68: write("") jpayne@68: write(_strftime(value)) jpayne@68: write("\n") jpayne@68: dispatch[datetime] = dump_datetime jpayne@68: jpayne@68: def dump_instance(self, value, write): jpayne@68: # check for special wrappers jpayne@68: if value.__class__ in WRAPPERS: jpayne@68: self.write = write jpayne@68: value.encode(self) jpayne@68: del self.write jpayne@68: else: jpayne@68: # store instance attributes as a struct (really?) jpayne@68: self.dump_struct(value.__dict__, write) jpayne@68: dispatch[DateTime] = dump_instance jpayne@68: dispatch[Binary] = dump_instance jpayne@68: # XXX(twouters): using "_arbitrary_instance" as key as a quick-fix jpayne@68: # for the p3yk merge, this should probably be fixed more neatly. jpayne@68: dispatch["_arbitrary_instance"] = dump_instance jpayne@68: jpayne@68: ## jpayne@68: # XML-RPC unmarshaller. jpayne@68: # jpayne@68: # @see loads jpayne@68: jpayne@68: class Unmarshaller: jpayne@68: """Unmarshal an XML-RPC response, based on incoming XML event jpayne@68: messages (start, data, end). Call close() to get the resulting jpayne@68: data structure. jpayne@68: jpayne@68: Note that this reader is fairly tolerant, and gladly accepts bogus jpayne@68: XML-RPC data without complaining (but not bogus XML). jpayne@68: """ jpayne@68: jpayne@68: # and again, if you don't understand what's going on in here, jpayne@68: # that's perfectly ok. jpayne@68: jpayne@68: def __init__(self, use_datetime=False, use_builtin_types=False): jpayne@68: self._type = None jpayne@68: self._stack = [] jpayne@68: self._marks = [] jpayne@68: self._data = [] jpayne@68: self._value = False jpayne@68: self._methodname = None jpayne@68: self._encoding = "utf-8" jpayne@68: self.append = self._stack.append jpayne@68: self._use_datetime = use_builtin_types or use_datetime jpayne@68: self._use_bytes = use_builtin_types jpayne@68: jpayne@68: def close(self): jpayne@68: # return response tuple and target method jpayne@68: if self._type is None or self._marks: jpayne@68: raise ResponseError() jpayne@68: if self._type == "fault": jpayne@68: raise Fault(**self._stack[0]) jpayne@68: return tuple(self._stack) jpayne@68: jpayne@68: def getmethodname(self): jpayne@68: return self._methodname jpayne@68: jpayne@68: # jpayne@68: # event handlers jpayne@68: jpayne@68: def xml(self, encoding, standalone): jpayne@68: self._encoding = encoding jpayne@68: # FIXME: assert standalone == 1 ??? jpayne@68: jpayne@68: def start(self, tag, attrs): jpayne@68: # prepare to handle this element jpayne@68: if ':' in tag: jpayne@68: tag = tag.split(':')[-1] jpayne@68: if tag == "array" or tag == "struct": jpayne@68: self._marks.append(len(self._stack)) jpayne@68: self._data = [] jpayne@68: if self._value and tag not in self.dispatch: jpayne@68: raise ResponseError("unknown tag %r" % tag) jpayne@68: self._value = (tag == "value") jpayne@68: jpayne@68: def data(self, text): jpayne@68: self._data.append(text) jpayne@68: jpayne@68: def end(self, tag): jpayne@68: # call the appropriate end tag handler jpayne@68: try: jpayne@68: f = self.dispatch[tag] jpayne@68: except KeyError: jpayne@68: if ':' not in tag: jpayne@68: return # unknown tag ? jpayne@68: try: jpayne@68: f = self.dispatch[tag.split(':')[-1]] jpayne@68: except KeyError: jpayne@68: return # unknown tag ? jpayne@68: return f(self, "".join(self._data)) jpayne@68: jpayne@68: # jpayne@68: # accelerator support jpayne@68: jpayne@68: def end_dispatch(self, tag, data): jpayne@68: # dispatch data jpayne@68: try: jpayne@68: f = self.dispatch[tag] jpayne@68: except KeyError: jpayne@68: if ':' not in tag: jpayne@68: return # unknown tag ? jpayne@68: try: jpayne@68: f = self.dispatch[tag.split(':')[-1]] jpayne@68: except KeyError: jpayne@68: return # unknown tag ? jpayne@68: return f(self, data) jpayne@68: jpayne@68: # jpayne@68: # element decoders jpayne@68: jpayne@68: dispatch = {} jpayne@68: jpayne@68: def end_nil (self, data): jpayne@68: self.append(None) jpayne@68: self._value = 0 jpayne@68: dispatch["nil"] = end_nil jpayne@68: jpayne@68: def end_boolean(self, data): jpayne@68: if data == "0": jpayne@68: self.append(False) jpayne@68: elif data == "1": jpayne@68: self.append(True) jpayne@68: else: jpayne@68: raise TypeError("bad boolean value") jpayne@68: self._value = 0 jpayne@68: dispatch["boolean"] = end_boolean jpayne@68: jpayne@68: def end_int(self, data): jpayne@68: self.append(int(data)) jpayne@68: self._value = 0 jpayne@68: dispatch["i1"] = end_int jpayne@68: dispatch["i2"] = end_int jpayne@68: dispatch["i4"] = end_int jpayne@68: dispatch["i8"] = end_int jpayne@68: dispatch["int"] = end_int jpayne@68: dispatch["biginteger"] = end_int jpayne@68: jpayne@68: def end_double(self, data): jpayne@68: self.append(float(data)) jpayne@68: self._value = 0 jpayne@68: dispatch["double"] = end_double jpayne@68: dispatch["float"] = end_double jpayne@68: jpayne@68: def end_bigdecimal(self, data): jpayne@68: self.append(Decimal(data)) jpayne@68: self._value = 0 jpayne@68: dispatch["bigdecimal"] = end_bigdecimal jpayne@68: jpayne@68: def end_string(self, data): jpayne@68: if self._encoding: jpayne@68: data = data.decode(self._encoding) jpayne@68: self.append(data) jpayne@68: self._value = 0 jpayne@68: dispatch["string"] = end_string jpayne@68: dispatch["name"] = end_string # struct keys are always strings jpayne@68: jpayne@68: def end_array(self, data): jpayne@68: mark = self._marks.pop() jpayne@68: # map arrays to Python lists jpayne@68: self._stack[mark:] = [self._stack[mark:]] jpayne@68: self._value = 0 jpayne@68: dispatch["array"] = end_array jpayne@68: jpayne@68: def end_struct(self, data): jpayne@68: mark = self._marks.pop() jpayne@68: # map structs to Python dictionaries jpayne@68: dict = {} jpayne@68: items = self._stack[mark:] jpayne@68: for i in range(0, len(items), 2): jpayne@68: dict[items[i]] = items[i+1] jpayne@68: self._stack[mark:] = [dict] jpayne@68: self._value = 0 jpayne@68: dispatch["struct"] = end_struct jpayne@68: jpayne@68: def end_base64(self, data): jpayne@68: value = Binary() jpayne@68: value.decode(data.encode("ascii")) jpayne@68: if self._use_bytes: jpayne@68: value = value.data jpayne@68: self.append(value) jpayne@68: self._value = 0 jpayne@68: dispatch["base64"] = end_base64 jpayne@68: jpayne@68: def end_dateTime(self, data): jpayne@68: value = DateTime() jpayne@68: value.decode(data) jpayne@68: if self._use_datetime: jpayne@68: value = _datetime_type(data) jpayne@68: self.append(value) jpayne@68: dispatch["dateTime.iso8601"] = end_dateTime jpayne@68: jpayne@68: def end_value(self, data): jpayne@68: # if we stumble upon a value element with no internal jpayne@68: # elements, treat it as a string element jpayne@68: if self._value: jpayne@68: self.end_string(data) jpayne@68: dispatch["value"] = end_value jpayne@68: jpayne@68: def end_params(self, data): jpayne@68: self._type = "params" jpayne@68: dispatch["params"] = end_params jpayne@68: jpayne@68: def end_fault(self, data): jpayne@68: self._type = "fault" jpayne@68: dispatch["fault"] = end_fault jpayne@68: jpayne@68: def end_methodName(self, data): jpayne@68: if self._encoding: jpayne@68: data = data.decode(self._encoding) jpayne@68: self._methodname = data jpayne@68: self._type = "methodName" # no params jpayne@68: dispatch["methodName"] = end_methodName jpayne@68: jpayne@68: ## Multicall support jpayne@68: # jpayne@68: jpayne@68: class _MultiCallMethod: jpayne@68: # some lesser magic to store calls made to a MultiCall object jpayne@68: # for batch execution jpayne@68: def __init__(self, call_list, name): jpayne@68: self.__call_list = call_list jpayne@68: self.__name = name jpayne@68: def __getattr__(self, name): jpayne@68: return _MultiCallMethod(self.__call_list, "%s.%s" % (self.__name, name)) jpayne@68: def __call__(self, *args): jpayne@68: self.__call_list.append((self.__name, args)) jpayne@68: jpayne@68: class MultiCallIterator: jpayne@68: """Iterates over the results of a multicall. Exceptions are jpayne@68: raised in response to xmlrpc faults.""" jpayne@68: jpayne@68: def __init__(self, results): jpayne@68: self.results = results jpayne@68: jpayne@68: def __getitem__(self, i): jpayne@68: item = self.results[i] jpayne@68: if type(item) == type({}): jpayne@68: raise Fault(item['faultCode'], item['faultString']) jpayne@68: elif type(item) == type([]): jpayne@68: return item[0] jpayne@68: else: jpayne@68: raise ValueError("unexpected type in multicall result") jpayne@68: jpayne@68: class MultiCall: jpayne@68: """server -> an object used to boxcar method calls jpayne@68: jpayne@68: server should be a ServerProxy object. jpayne@68: jpayne@68: Methods can be added to the MultiCall using normal jpayne@68: method call syntax e.g.: jpayne@68: jpayne@68: multicall = MultiCall(server_proxy) jpayne@68: multicall.add(2,3) jpayne@68: multicall.get_address("Guido") jpayne@68: jpayne@68: To execute the multicall, call the MultiCall object e.g.: jpayne@68: jpayne@68: add_result, address = multicall() jpayne@68: """ jpayne@68: jpayne@68: def __init__(self, server): jpayne@68: self.__server = server jpayne@68: self.__call_list = [] jpayne@68: jpayne@68: def __repr__(self): jpayne@68: return "<%s at %#x>" % (self.__class__.__name__, id(self)) jpayne@68: jpayne@68: def __getattr__(self, name): jpayne@68: return _MultiCallMethod(self.__call_list, name) jpayne@68: jpayne@68: def __call__(self): jpayne@68: marshalled_list = [] jpayne@68: for name, args in self.__call_list: jpayne@68: marshalled_list.append({'methodName' : name, 'params' : args}) jpayne@68: jpayne@68: return MultiCallIterator(self.__server.system.multicall(marshalled_list)) jpayne@68: jpayne@68: # -------------------------------------------------------------------- jpayne@68: # convenience functions jpayne@68: jpayne@68: FastMarshaller = FastParser = FastUnmarshaller = None jpayne@68: jpayne@68: ## jpayne@68: # Create a parser object, and connect it to an unmarshalling instance. jpayne@68: # This function picks the fastest available XML parser. jpayne@68: # jpayne@68: # return A (parser, unmarshaller) tuple. jpayne@68: jpayne@68: def getparser(use_datetime=False, use_builtin_types=False): jpayne@68: """getparser() -> parser, unmarshaller jpayne@68: jpayne@68: Create an instance of the fastest available parser, and attach it jpayne@68: to an unmarshalling object. Return both objects. jpayne@68: """ jpayne@68: if FastParser and FastUnmarshaller: jpayne@68: if use_builtin_types: jpayne@68: mkdatetime = _datetime_type jpayne@68: mkbytes = base64.decodebytes jpayne@68: elif use_datetime: jpayne@68: mkdatetime = _datetime_type jpayne@68: mkbytes = _binary jpayne@68: else: jpayne@68: mkdatetime = _datetime jpayne@68: mkbytes = _binary jpayne@68: target = FastUnmarshaller(True, False, mkbytes, mkdatetime, Fault) jpayne@68: parser = FastParser(target) jpayne@68: else: jpayne@68: target = Unmarshaller(use_datetime=use_datetime, use_builtin_types=use_builtin_types) jpayne@68: if FastParser: jpayne@68: parser = FastParser(target) jpayne@68: else: jpayne@68: parser = ExpatParser(target) jpayne@68: return parser, target jpayne@68: jpayne@68: ## jpayne@68: # Convert a Python tuple or a Fault instance to an XML-RPC packet. jpayne@68: # jpayne@68: # @def dumps(params, **options) jpayne@68: # @param params A tuple or Fault instance. jpayne@68: # @keyparam methodname If given, create a methodCall request for jpayne@68: # this method name. jpayne@68: # @keyparam methodresponse If given, create a methodResponse packet. jpayne@68: # If used with a tuple, the tuple must be a singleton (that is, jpayne@68: # it must contain exactly one element). jpayne@68: # @keyparam encoding The packet encoding. jpayne@68: # @return A string containing marshalled data. jpayne@68: jpayne@68: def dumps(params, methodname=None, methodresponse=None, encoding=None, jpayne@68: allow_none=False): jpayne@68: """data [,options] -> marshalled data jpayne@68: jpayne@68: Convert an argument tuple or a Fault instance to an XML-RPC jpayne@68: request (or response, if the methodresponse option is used). jpayne@68: jpayne@68: In addition to the data object, the following options can be given jpayne@68: as keyword arguments: jpayne@68: jpayne@68: methodname: the method name for a methodCall packet jpayne@68: jpayne@68: methodresponse: true to create a methodResponse packet. jpayne@68: If this option is used with a tuple, the tuple must be jpayne@68: a singleton (i.e. it can contain only one element). jpayne@68: jpayne@68: encoding: the packet encoding (default is UTF-8) jpayne@68: jpayne@68: All byte strings in the data structure are assumed to use the jpayne@68: packet encoding. Unicode strings are automatically converted, jpayne@68: where necessary. jpayne@68: """ jpayne@68: jpayne@68: assert isinstance(params, (tuple, Fault)), "argument must be tuple or Fault instance" jpayne@68: if isinstance(params, Fault): jpayne@68: methodresponse = 1 jpayne@68: elif methodresponse and isinstance(params, tuple): jpayne@68: assert len(params) == 1, "response tuple must be a singleton" jpayne@68: jpayne@68: if not encoding: jpayne@68: encoding = "utf-8" jpayne@68: jpayne@68: if FastMarshaller: jpayne@68: m = FastMarshaller(encoding) jpayne@68: else: jpayne@68: m = Marshaller(encoding, allow_none) jpayne@68: jpayne@68: data = m.dumps(params) jpayne@68: jpayne@68: if encoding != "utf-8": jpayne@68: xmlheader = "\n" % str(encoding) jpayne@68: else: jpayne@68: xmlheader = "\n" # utf-8 is default jpayne@68: jpayne@68: # standard XML-RPC wrappings jpayne@68: if methodname: jpayne@68: # a method call jpayne@68: data = ( jpayne@68: xmlheader, jpayne@68: "\n" jpayne@68: "", methodname, "\n", jpayne@68: data, jpayne@68: "\n" jpayne@68: ) jpayne@68: elif methodresponse: jpayne@68: # a method response, or a fault structure jpayne@68: data = ( jpayne@68: xmlheader, jpayne@68: "\n", jpayne@68: data, jpayne@68: "\n" jpayne@68: ) jpayne@68: else: jpayne@68: return data # return as is jpayne@68: return "".join(data) jpayne@68: jpayne@68: ## jpayne@68: # Convert an XML-RPC packet to a Python object. If the XML-RPC packet jpayne@68: # represents a fault condition, this function raises a Fault exception. jpayne@68: # jpayne@68: # @param data An XML-RPC packet, given as an 8-bit string. jpayne@68: # @return A tuple containing the unpacked data, and the method name jpayne@68: # (None if not present). jpayne@68: # @see Fault jpayne@68: jpayne@68: def loads(data, use_datetime=False, use_builtin_types=False): jpayne@68: """data -> unmarshalled data, method name jpayne@68: jpayne@68: Convert an XML-RPC packet to unmarshalled data plus a method jpayne@68: name (None if not present). jpayne@68: jpayne@68: If the XML-RPC packet represents a fault condition, this function jpayne@68: raises a Fault exception. jpayne@68: """ jpayne@68: p, u = getparser(use_datetime=use_datetime, use_builtin_types=use_builtin_types) jpayne@68: p.feed(data) jpayne@68: p.close() jpayne@68: return u.close(), u.getmethodname() jpayne@68: jpayne@68: ## jpayne@68: # Encode a string using the gzip content encoding such as specified by the jpayne@68: # Content-Encoding: gzip jpayne@68: # in the HTTP header, as described in RFC 1952 jpayne@68: # jpayne@68: # @param data the unencoded data jpayne@68: # @return the encoded data jpayne@68: jpayne@68: def gzip_encode(data): jpayne@68: """data -> gzip encoded data jpayne@68: jpayne@68: Encode data using the gzip content encoding as described in RFC 1952 jpayne@68: """ jpayne@68: if not gzip: jpayne@68: raise NotImplementedError jpayne@68: f = BytesIO() jpayne@68: with gzip.GzipFile(mode="wb", fileobj=f, compresslevel=1) as gzf: jpayne@68: gzf.write(data) jpayne@68: return f.getvalue() jpayne@68: jpayne@68: ## jpayne@68: # Decode a string using the gzip content encoding such as specified by the jpayne@68: # Content-Encoding: gzip jpayne@68: # in the HTTP header, as described in RFC 1952 jpayne@68: # jpayne@68: # @param data The encoded data jpayne@68: # @keyparam max_decode Maximum bytes to decode (20 MiB default), use negative jpayne@68: # values for unlimited decoding jpayne@68: # @return the unencoded data jpayne@68: # @raises ValueError if data is not correctly coded. jpayne@68: # @raises ValueError if max gzipped payload length exceeded jpayne@68: jpayne@68: def gzip_decode(data, max_decode=20971520): jpayne@68: """gzip encoded data -> unencoded data jpayne@68: jpayne@68: Decode data using the gzip content encoding as described in RFC 1952 jpayne@68: """ jpayne@68: if not gzip: jpayne@68: raise NotImplementedError jpayne@68: with gzip.GzipFile(mode="rb", fileobj=BytesIO(data)) as gzf: jpayne@68: try: jpayne@68: if max_decode < 0: # no limit jpayne@68: decoded = gzf.read() jpayne@68: else: jpayne@68: decoded = gzf.read(max_decode + 1) jpayne@68: except OSError: jpayne@68: raise ValueError("invalid data") jpayne@68: if max_decode >= 0 and len(decoded) > max_decode: jpayne@68: raise ValueError("max gzipped payload length exceeded") jpayne@68: return decoded jpayne@68: jpayne@68: ## jpayne@68: # Return a decoded file-like object for the gzip encoding jpayne@68: # as described in RFC 1952. jpayne@68: # jpayne@68: # @param response A stream supporting a read() method jpayne@68: # @return a file-like object that the decoded data can be read() from jpayne@68: jpayne@68: class GzipDecodedResponse(gzip.GzipFile if gzip else object): jpayne@68: """a file-like object to decode a response encoded with the gzip jpayne@68: method, as described in RFC 1952. jpayne@68: """ jpayne@68: def __init__(self, response): jpayne@68: #response doesn't support tell() and read(), required by jpayne@68: #GzipFile jpayne@68: if not gzip: jpayne@68: raise NotImplementedError jpayne@68: self.io = BytesIO(response.read()) jpayne@68: gzip.GzipFile.__init__(self, mode="rb", fileobj=self.io) jpayne@68: jpayne@68: def close(self): jpayne@68: try: jpayne@68: gzip.GzipFile.close(self) jpayne@68: finally: jpayne@68: self.io.close() jpayne@68: jpayne@68: jpayne@68: # -------------------------------------------------------------------- jpayne@68: # request dispatcher jpayne@68: jpayne@68: class _Method: jpayne@68: # some magic to bind an XML-RPC method to an RPC server. jpayne@68: # supports "nested" methods (e.g. examples.getStateName) jpayne@68: def __init__(self, send, name): jpayne@68: self.__send = send jpayne@68: self.__name = name jpayne@68: def __getattr__(self, name): jpayne@68: return _Method(self.__send, "%s.%s" % (self.__name, name)) jpayne@68: def __call__(self, *args): jpayne@68: return self.__send(self.__name, args) jpayne@68: jpayne@68: ## jpayne@68: # Standard transport class for XML-RPC over HTTP. jpayne@68: #

jpayne@68: # You can create custom transports by subclassing this method, and jpayne@68: # overriding selected methods. jpayne@68: jpayne@68: class Transport: jpayne@68: """Handles an HTTP transaction to an XML-RPC server.""" jpayne@68: jpayne@68: # client identifier (may be overridden) jpayne@68: user_agent = "Python-xmlrpc/%s" % __version__ jpayne@68: jpayne@68: #if true, we'll request gzip encoding jpayne@68: accept_gzip_encoding = True jpayne@68: jpayne@68: # if positive, encode request using gzip if it exceeds this threshold jpayne@68: # note that many servers will get confused, so only use it if you know jpayne@68: # that they can decode such a request jpayne@68: encode_threshold = None #None = don't encode jpayne@68: jpayne@68: def __init__(self, use_datetime=False, use_builtin_types=False, jpayne@68: *, headers=()): jpayne@68: self._use_datetime = use_datetime jpayne@68: self._use_builtin_types = use_builtin_types jpayne@68: self._connection = (None, None) jpayne@68: self._headers = list(headers) jpayne@68: self._extra_headers = [] jpayne@68: jpayne@68: ## jpayne@68: # Send a complete request, and parse the response. jpayne@68: # Retry request if a cached connection has disconnected. jpayne@68: # jpayne@68: # @param host Target host. jpayne@68: # @param handler Target PRC handler. jpayne@68: # @param request_body XML-RPC request body. jpayne@68: # @param verbose Debugging flag. jpayne@68: # @return Parsed response. jpayne@68: jpayne@68: def request(self, host, handler, request_body, verbose=False): jpayne@68: #retry request once if cached connection has gone cold jpayne@68: for i in (0, 1): jpayne@68: try: jpayne@68: return self.single_request(host, handler, request_body, verbose) jpayne@68: except http.client.RemoteDisconnected: jpayne@68: if i: jpayne@68: raise jpayne@68: except OSError as e: jpayne@68: if i or e.errno not in (errno.ECONNRESET, errno.ECONNABORTED, jpayne@68: errno.EPIPE): jpayne@68: raise jpayne@68: jpayne@68: def single_request(self, host, handler, request_body, verbose=False): jpayne@68: # issue XML-RPC request jpayne@68: try: jpayne@68: http_conn = self.send_request(host, handler, request_body, verbose) jpayne@68: resp = http_conn.getresponse() jpayne@68: if resp.status == 200: jpayne@68: self.verbose = verbose jpayne@68: return self.parse_response(resp) jpayne@68: jpayne@68: except Fault: jpayne@68: raise jpayne@68: except Exception: jpayne@68: #All unexpected errors leave connection in jpayne@68: # a strange state, so we clear it. jpayne@68: self.close() jpayne@68: raise jpayne@68: jpayne@68: #We got an error response. jpayne@68: #Discard any response data and raise exception jpayne@68: if resp.getheader("content-length", ""): jpayne@68: resp.read() jpayne@68: raise ProtocolError( jpayne@68: host + handler, jpayne@68: resp.status, resp.reason, jpayne@68: dict(resp.getheaders()) jpayne@68: ) jpayne@68: jpayne@68: jpayne@68: ## jpayne@68: # Create parser. jpayne@68: # jpayne@68: # @return A 2-tuple containing a parser and an unmarshaller. jpayne@68: jpayne@68: def getparser(self): jpayne@68: # get parser and unmarshaller jpayne@68: return getparser(use_datetime=self._use_datetime, jpayne@68: use_builtin_types=self._use_builtin_types) jpayne@68: jpayne@68: ## jpayne@68: # Get authorization info from host parameter jpayne@68: # Host may be a string, or a (host, x509-dict) tuple; if a string, jpayne@68: # it is checked for a "user:pw@host" format, and a "Basic jpayne@68: # Authentication" header is added if appropriate. jpayne@68: # jpayne@68: # @param host Host descriptor (URL or (URL, x509 info) tuple). jpayne@68: # @return A 3-tuple containing (actual host, extra headers, jpayne@68: # x509 info). The header and x509 fields may be None. jpayne@68: jpayne@68: def get_host_info(self, host): jpayne@68: jpayne@68: x509 = {} jpayne@68: if isinstance(host, tuple): jpayne@68: host, x509 = host jpayne@68: jpayne@68: auth, host = urllib.parse._splituser(host) jpayne@68: jpayne@68: if auth: jpayne@68: auth = urllib.parse.unquote_to_bytes(auth) jpayne@68: auth = base64.encodebytes(auth).decode("utf-8") jpayne@68: auth = "".join(auth.split()) # get rid of whitespace jpayne@68: extra_headers = [ jpayne@68: ("Authorization", "Basic " + auth) jpayne@68: ] jpayne@68: else: jpayne@68: extra_headers = [] jpayne@68: jpayne@68: return host, extra_headers, x509 jpayne@68: jpayne@68: ## jpayne@68: # Connect to server. jpayne@68: # jpayne@68: # @param host Target host. jpayne@68: # @return An HTTPConnection object jpayne@68: jpayne@68: def make_connection(self, host): jpayne@68: #return an existing connection if possible. This allows jpayne@68: #HTTP/1.1 keep-alive. jpayne@68: if self._connection and host == self._connection[0]: jpayne@68: return self._connection[1] jpayne@68: # create a HTTP connection object from a host descriptor jpayne@68: chost, self._extra_headers, x509 = self.get_host_info(host) jpayne@68: self._connection = host, http.client.HTTPConnection(chost) jpayne@68: return self._connection[1] jpayne@68: jpayne@68: ## jpayne@68: # Clear any cached connection object. jpayne@68: # Used in the event of socket errors. jpayne@68: # jpayne@68: def close(self): jpayne@68: host, connection = self._connection jpayne@68: if connection: jpayne@68: self._connection = (None, None) jpayne@68: connection.close() jpayne@68: jpayne@68: ## jpayne@68: # Send HTTP request. jpayne@68: # jpayne@68: # @param host Host descriptor (URL or (URL, x509 info) tuple). jpayne@68: # @param handler Target RPC handler (a path relative to host) jpayne@68: # @param request_body The XML-RPC request body jpayne@68: # @param debug Enable debugging if debug is true. jpayne@68: # @return An HTTPConnection. jpayne@68: jpayne@68: def send_request(self, host, handler, request_body, debug): jpayne@68: connection = self.make_connection(host) jpayne@68: headers = self._headers + self._extra_headers jpayne@68: if debug: jpayne@68: connection.set_debuglevel(1) jpayne@68: if self.accept_gzip_encoding and gzip: jpayne@68: connection.putrequest("POST", handler, skip_accept_encoding=True) jpayne@68: headers.append(("Accept-Encoding", "gzip")) jpayne@68: else: jpayne@68: connection.putrequest("POST", handler) jpayne@68: headers.append(("Content-Type", "text/xml")) jpayne@68: headers.append(("User-Agent", self.user_agent)) jpayne@68: self.send_headers(connection, headers) jpayne@68: self.send_content(connection, request_body) jpayne@68: return connection jpayne@68: jpayne@68: ## jpayne@68: # Send request headers. jpayne@68: # This function provides a useful hook for subclassing jpayne@68: # jpayne@68: # @param connection httpConnection. jpayne@68: # @param headers list of key,value pairs for HTTP headers jpayne@68: jpayne@68: def send_headers(self, connection, headers): jpayne@68: for key, val in headers: jpayne@68: connection.putheader(key, val) jpayne@68: jpayne@68: ## jpayne@68: # Send request body. jpayne@68: # This function provides a useful hook for subclassing jpayne@68: # jpayne@68: # @param connection httpConnection. jpayne@68: # @param request_body XML-RPC request body. jpayne@68: jpayne@68: def send_content(self, connection, request_body): jpayne@68: #optionally encode the request jpayne@68: if (self.encode_threshold is not None and jpayne@68: self.encode_threshold < len(request_body) and jpayne@68: gzip): jpayne@68: connection.putheader("Content-Encoding", "gzip") jpayne@68: request_body = gzip_encode(request_body) jpayne@68: jpayne@68: connection.putheader("Content-Length", str(len(request_body))) jpayne@68: connection.endheaders(request_body) jpayne@68: jpayne@68: ## jpayne@68: # Parse response. jpayne@68: # jpayne@68: # @param file Stream. jpayne@68: # @return Response tuple and target method. jpayne@68: jpayne@68: def parse_response(self, response): jpayne@68: # read response data from httpresponse, and parse it jpayne@68: # Check for new http response object, otherwise it is a file object. jpayne@68: if hasattr(response, 'getheader'): jpayne@68: if response.getheader("Content-Encoding", "") == "gzip": jpayne@68: stream = GzipDecodedResponse(response) jpayne@68: else: jpayne@68: stream = response jpayne@68: else: jpayne@68: stream = response jpayne@68: jpayne@68: p, u = self.getparser() jpayne@68: jpayne@68: while 1: jpayne@68: data = stream.read(1024) jpayne@68: if not data: jpayne@68: break jpayne@68: if self.verbose: jpayne@68: print("body:", repr(data)) jpayne@68: p.feed(data) jpayne@68: jpayne@68: if stream is not response: jpayne@68: stream.close() jpayne@68: p.close() jpayne@68: jpayne@68: return u.close() jpayne@68: jpayne@68: ## jpayne@68: # Standard transport class for XML-RPC over HTTPS. jpayne@68: jpayne@68: class SafeTransport(Transport): jpayne@68: """Handles an HTTPS transaction to an XML-RPC server.""" jpayne@68: jpayne@68: def __init__(self, use_datetime=False, use_builtin_types=False, jpayne@68: *, headers=(), context=None): jpayne@68: super().__init__(use_datetime=use_datetime, jpayne@68: use_builtin_types=use_builtin_types, jpayne@68: headers=headers) jpayne@68: self.context = context jpayne@68: jpayne@68: # FIXME: mostly untested jpayne@68: jpayne@68: def make_connection(self, host): jpayne@68: if self._connection and host == self._connection[0]: jpayne@68: return self._connection[1] jpayne@68: jpayne@68: if not hasattr(http.client, "HTTPSConnection"): jpayne@68: raise NotImplementedError( jpayne@68: "your version of http.client doesn't support HTTPS") jpayne@68: # create a HTTPS connection object from a host descriptor jpayne@68: # host may be a string, or a (host, x509-dict) tuple jpayne@68: chost, self._extra_headers, x509 = self.get_host_info(host) jpayne@68: self._connection = host, http.client.HTTPSConnection(chost, jpayne@68: None, context=self.context, **(x509 or {})) jpayne@68: return self._connection[1] jpayne@68: jpayne@68: ## jpayne@68: # Standard server proxy. This class establishes a virtual connection jpayne@68: # to an XML-RPC server. jpayne@68: #

jpayne@68: # This class is available as ServerProxy and Server. New code should jpayne@68: # use ServerProxy, to avoid confusion. jpayne@68: # jpayne@68: # @def ServerProxy(uri, **options) jpayne@68: # @param uri The connection point on the server. jpayne@68: # @keyparam transport A transport factory, compatible with the jpayne@68: # standard transport class. jpayne@68: # @keyparam encoding The default encoding used for 8-bit strings jpayne@68: # (default is UTF-8). jpayne@68: # @keyparam verbose Use a true value to enable debugging output. jpayne@68: # (printed to standard output). jpayne@68: # @see Transport jpayne@68: jpayne@68: class ServerProxy: jpayne@68: """uri [,options] -> a logical connection to an XML-RPC server jpayne@68: jpayne@68: uri is the connection point on the server, given as jpayne@68: scheme://host/target. jpayne@68: jpayne@68: The standard implementation always supports the "http" scheme. If jpayne@68: SSL socket support is available (Python 2.0), it also supports jpayne@68: "https". jpayne@68: jpayne@68: If the target part and the slash preceding it are both omitted, jpayne@68: "/RPC2" is assumed. jpayne@68: jpayne@68: The following options can be given as keyword arguments: jpayne@68: jpayne@68: transport: a transport factory jpayne@68: encoding: the request encoding (default is UTF-8) jpayne@68: jpayne@68: All 8-bit strings passed to the server proxy are assumed to use jpayne@68: the given encoding. jpayne@68: """ jpayne@68: jpayne@68: def __init__(self, uri, transport=None, encoding=None, verbose=False, jpayne@68: allow_none=False, use_datetime=False, use_builtin_types=False, jpayne@68: *, headers=(), context=None): jpayne@68: # establish a "logical" server connection jpayne@68: jpayne@68: # get the url jpayne@68: type, uri = urllib.parse._splittype(uri) jpayne@68: if type not in ("http", "https"): jpayne@68: raise OSError("unsupported XML-RPC protocol") jpayne@68: self.__host, self.__handler = urllib.parse._splithost(uri) jpayne@68: if not self.__handler: jpayne@68: self.__handler = "/RPC2" jpayne@68: jpayne@68: if transport is None: jpayne@68: if type == "https": jpayne@68: handler = SafeTransport jpayne@68: extra_kwargs = {"context": context} jpayne@68: else: jpayne@68: handler = Transport jpayne@68: extra_kwargs = {} jpayne@68: transport = handler(use_datetime=use_datetime, jpayne@68: use_builtin_types=use_builtin_types, jpayne@68: headers=headers, jpayne@68: **extra_kwargs) jpayne@68: self.__transport = transport jpayne@68: jpayne@68: self.__encoding = encoding or 'utf-8' jpayne@68: self.__verbose = verbose jpayne@68: self.__allow_none = allow_none jpayne@68: jpayne@68: def __close(self): jpayne@68: self.__transport.close() jpayne@68: jpayne@68: def __request(self, methodname, params): jpayne@68: # call a method on the remote server jpayne@68: jpayne@68: request = dumps(params, methodname, encoding=self.__encoding, jpayne@68: allow_none=self.__allow_none).encode(self.__encoding, 'xmlcharrefreplace') jpayne@68: jpayne@68: response = self.__transport.request( jpayne@68: self.__host, jpayne@68: self.__handler, jpayne@68: request, jpayne@68: verbose=self.__verbose jpayne@68: ) jpayne@68: jpayne@68: if len(response) == 1: jpayne@68: response = response[0] jpayne@68: jpayne@68: return response jpayne@68: jpayne@68: def __repr__(self): jpayne@68: return ( jpayne@68: "<%s for %s%s>" % jpayne@68: (self.__class__.__name__, self.__host, self.__handler) jpayne@68: ) jpayne@68: jpayne@68: def __getattr__(self, name): jpayne@68: # magic method dispatcher jpayne@68: return _Method(self.__request, name) jpayne@68: jpayne@68: # note: to call a remote object with a non-standard name, use jpayne@68: # result getattr(server, "strange-python-name")(args) jpayne@68: jpayne@68: def __call__(self, attr): jpayne@68: """A workaround to get special attributes on the ServerProxy jpayne@68: without interfering with the magic __getattr__ jpayne@68: """ jpayne@68: if attr == "close": jpayne@68: return self.__close jpayne@68: elif attr == "transport": jpayne@68: return self.__transport jpayne@68: raise AttributeError("Attribute %r not found" % (attr,)) jpayne@68: jpayne@68: def __enter__(self): jpayne@68: return self jpayne@68: jpayne@68: def __exit__(self, *args): jpayne@68: self.__close() jpayne@68: jpayne@68: # compatibility jpayne@68: jpayne@68: Server = ServerProxy jpayne@68: jpayne@68: # -------------------------------------------------------------------- jpayne@68: # test code jpayne@68: jpayne@68: if __name__ == "__main__": jpayne@68: jpayne@68: # simple test program (from the XML-RPC specification) jpayne@68: jpayne@68: # local server, available from Lib/xmlrpc/server.py jpayne@68: server = ServerProxy("http://localhost:8000") jpayne@68: jpayne@68: try: jpayne@68: print(server.currentTime.getCurrentTime()) jpayne@68: except Error as v: jpayne@68: print("ERROR", v) jpayne@68: jpayne@68: multi = MultiCall(server) jpayne@68: multi.getData() jpayne@68: multi.pow(2,9) jpayne@68: multi.add(1,2) jpayne@68: try: jpayne@68: for response in multi(): jpayne@68: print(response) jpayne@68: except Error as v: jpayne@68: print("ERROR", v)