Lines Matching +full:write +full:- +full:host
2 # XML-RPC CLIENT LIBRARY
5 # an XML-RPC client interface for Python.
8 # implement XML-RPC servers.
14 # 1999-01-14 fl Created
15 # 1999-01-15 fl Changed dateTime to use localtime
16 # 1999-01-16 fl Added Binary/base64 element, default to RPC2 service
17 # 1999-01-19 fl Fixed array data element (from Skip Montanaro)
18 # 1999-01-21 fl Fixed dateTime constructor, etc.
19 # 1999-02-02 fl Added fault handling, handle empty sequences, etc.
20 # 1999-02-10 fl Fixed problem with empty responses (from Skip Montanaro)
21 # 1999-06-20 fl Speed improvements, pluggable parsers/transports (0.9.8)
22 # 2000-11-28 fl Changed boolean to check the truth value of its argument
23 # 2001-02-24 fl Added encoding/Unicode/SafeTransport patches
24 # 2001-02-26 fl Added compare support to wrappers (0.9.9/1.0b1)
25 # 2001-03-28 fl Make sure response tuple is a singleton
26 # 2001-03-29 fl Don't require empty params element (from Nicholas Riley)
27 # 2001-06-10 fl Folded in _xmlrpclib accelerator support (1.0b2)
28 # 2001-08-20 fl Base xmlrpclib.Error on built-in Exception (from Paul Prescod)
29 # 2001-09-03 fl Allow Transport subclass to override getparser
30 # 2001-09-10 fl Lazy import of urllib, cgi, xmllib (20x import speedup)
31 # 2001-10-01 fl Remove containers from memo cache when done with them
32 # 2001-10-01 fl Use faster escape method (80% dumps speedup)
33 # 2001-10-02 fl More dumps microtuning
34 # 2001-10-04 fl Make sure import expat gets a parser (from Guido van Rossum)
35 # 2001-10-10 sm Allow long ints to be passed as ints if they don't overflow
36 # 2001-10-17 sm Test for int and long overflow (allows use on 64-bit systems)
37 # 2001-11-12 fl Use repr() to marshal doubles (from Paul Felix)
38 # 2002-03-17 fl Avoid buffered read when possible (from James Rucker)
39 # 2002-04-07 fl Added pythondoc comments
40 # 2002-04-16 fl Added __str__ methods to datetime/binary wrappers
41 # 2002-05-15 fl Added error constants (from Andrew Kuchling)
42 # 2002-06-27 fl Merged with Python CVS version
43 # 2002-10-22 fl Added basic authentication (based on code from Phillip Eby)
44 # 2003-01-22 sm Add support for the bool type
45 # 2003-02-27 gvr Remove apply calls
46 # 2003-04-24 sm Use cStringIO if available
47 # 2003-04-25 ak Add support for nil
48 # 2003-06-15 gn Add support for time.struct_time
49 # 2003-07-12 gp Correct marshalling of Faults
50 # 2003-10-31 mvl Add multicall support
51 # 2004-08-20 mvl Bump minimum supported Python version to 2.1
52 # 2014-12-02 ch/doko Add workaround for gzip bomb vulnerability
54 # Copyright (c) 1999-2002 by Secret Labs AB.
55 # Copyright (c) 1999-2002 by Fredrik Lundh.
60 # --------------------------------------------------------------------
61 # The XML-RPC client interface is
63 # Copyright (c) 1999-2002 by Secret Labs AB
64 # Copyright (c) 1999-2002 by Fredrik Lundh
80 # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-
87 # --------------------------------------------------------------------
95 An XML-RPC client interface for Python.
98 implement XML-RPC servers.
105 Fault Indicates an XML-RPC fault package
109 ServerProxy Represents a logical connection to an XML-RPC server
112 Boolean boolean wrapper to generate a "boolean" XML-RPC value
115 XML-RPC value
119 Marshaller Generate an XML-RPC params chunk from a Python data structure
120 Unmarshaller Unmarshal an XML-RPC response from incoming XML event message
121 Transport Handles an HTTP transaction to an XML-RPC server
122 SafeTransport Handles an HTTPS transaction to an XML-RPC server
131 boolean Convert any Python value to an XML-RPC boolean
134 dumps Convert an argument tuple or a Fault instance to an XML-RPC
136 loads Convert an XML-RPC packet to unmarshalled data plus a method
151 # --------------------------------------------------------------------
169 def _decode(data, encoding, is8bit=re.compile("[\x80-\xff]").search):
170 # decode non-ascii string (if possible)
182 # convert to 7-bit ascii if possible
194 MAXINT = 2L**31-1
195 MININT = -2L**31
197 # --------------------------------------------------------------------
199 # http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php)
202 PARSE_ERROR = -32700
203 SERVER_ERROR = -32600
204 APPLICATION_ERROR = -32500
205 SYSTEM_ERROR = -32400
206 TRANSPORT_ERROR = -32300
209 NOT_WELLFORMED_ERROR = -32700
210 UNSUPPORTED_ENCODING = -32701
211 INVALID_ENCODING_CHAR = -32702
212 INVALID_XMLRPC = -32600
213 METHOD_NOT_FOUND = -32601
214 INVALID_METHOD_PARAMS = -32602
215 INTERNAL_ERROR = -32603
217 # --------------------------------------------------------------------
221 # Base class for all kinds of client-side errors.
229 # Indicates an HTTP-level protocol error. This is raised by the HTTP
253 # Indicates a broken XML-RPC response package. This exception is
254 # raised by the unmarshalling layer, if the XML-RPC response is
262 # Indicates an XML-RPC fault response package. This exception is
263 # raised by the unmarshalling layer, if the XML-RPC response contains
265 # generate a fault XML-RPC message.
267 # @param faultCode The XML-RPC fault code.
268 # @param faultString The XML-RPC fault string.
271 """Indicates an XML-RPC fault package."""
282 # --------------------------------------------------------------------
286 # Wrapper for XML-RPC boolean values. Use the xmlrpclib.True and
288 # generate boolean XML-RPC values.
302 """Boolean-value wrapper.
304 Use True or False to generate a "boolean" XML-RPC value.
311 out.write("<value><boolean>%d</boolean></value>\n" % self.value)
334 # Map true or false value to XML-RPC boolean values.
345 """Convert any Python value to XML-RPC 'boolean'."""
351 # Wrapper for XML-RPC DateTime values. This converts a time value to
352 # the format used by XML-RPC.
355 # "yyyymmddThh:mm:ss", as a 9-item time tuple (as returned by
379 localtime integer value to generate 'dateTime.iso8601' XML-RPC
457 out.write("<value><dateTime.iso8601>")
458 out.write(self.value)
459 out.write("</dateTime.iso8601></value>\n")
473 # of binary data over XML-RPC, using BASE64 encoding.
475 # @param data An 8-bit string containing arbitrary data.
492 # @return Buffer contents, as an 8-bit string.
506 out.write("<value><base64>\n")
508 out.write("</base64></value>\n")
520 # --------------------------------------------------------------------
554 encoding = "utf-8"
584 xmllib.XMLParser.__init__(self) # pre-2.0
586 # --------------------------------------------------------------------
587 # XML-RPC marshalling and unmarshalling code
590 # XML-RPC marshaller.
592 # @param encoding Default encoding for 8-bit strings. The default
593 # value is None (interpreted as UTF-8).
597 """Generate an XML-RPC params chunk from a Python data structure.
601 to an XML-RPC params chunk. To write a fault response, pass a
619 write = out.append
623 write("<fault>\n")
626 write)
627 write("</fault>\n")
630 # FIXME: the xml-rpc specification allows us to leave out
635 write("<params>\n")
637 write("<param>\n")
638 dump(v, write)
639 write("</param>\n")
640 write("</params>\n")
644 def __dump(self, value, write): argument
653 # check if this class is a sub-class of a basic type,
655 # (e.g. a string sub-class)
660 f(self, value, write)
662 def dump_nil (self, value, write): argument
665 write("<value><nil/></value>")
668 def dump_int(self, value, write): argument
671 raise OverflowError, "int exceeds XML-RPC limits"
672 write("<value><int>")
673 write(str(value))
674 write("</int></value>\n")
678 def dump_bool(self, value, write): argument
679 write("<value><boolean>")
680 write(value and "1" or "0")
681 write("</boolean></value>\n")
684 def dump_long(self, value, write): argument
686 raise OverflowError, "long int exceeds XML-RPC limits"
687 write("<value><int>")
688 write(str(int(value)))
689 write("</int></value>\n")
692 def dump_double(self, value, write): argument
693 write("<value><double>")
694 write(repr(value))
695 write("</double></value>\n")
698 def dump_string(self, value, write, escape=escape): argument
699 write("<value><string>")
700 write(escape(value))
701 write("</string></value>\n")
705 def dump_unicode(self, value, write, escape=escape): argument
706 write("<value><string>")
707 write(escape(value).encode(self.encoding, 'xmlcharrefreplace'))
708 write("</string></value>\n")
711 def dump_array(self, value, write): argument
717 write("<value><array><data>\n")
719 dump(v, write)
720 write("</data></array></value>\n")
725 def dump_struct(self, value, write, escape=escape): argument
731 write("<value><struct>\n")
733 write("<member>\n")
740 write("<name>%s</name>\n" % k)
741 dump(v, write)
742 write("</member>\n")
743 write("</struct></value>\n")
748 def dump_datetime(self, value, write): argument
749 write("<value><dateTime.iso8601>")
750 write(_strftime(value))
751 write("</dateTime.iso8601></value>\n")
754 def dump_instance(self, value, write): argument
757 self.write = write
759 del self.write
762 self.dump_struct(value.__dict__, write)
766 # XML-RPC unmarshaller.
771 """Unmarshal an XML-RPC response, based on incoming XML event
776 XML-RPC data without complaining (but not bogus XML).
789 self._encoding = "utf-8"
973 """server -> an object used to boxcar method calls
1008 # --------------------------------------------------------------------
1018 """getparser() -> parser, unmarshaller
1043 # Convert a Python tuple or a Fault instance to an XML-RPC packet.
1057 """data [,options] -> marshalled data
1059 Convert an argument tuple or a Fault instance to an XML-RPC
1071 encoding: the packet encoding (default is UTF-8)
1073 All 8-bit strings in the data structure are assumed to use the
1087 encoding = "utf-8"
1096 if encoding != "utf-8":
1099 xmlheader = "<?xml version='1.0'?>\n" # utf-8 is default
1101 # standard XML-RPC wrappings
1126 # Convert an XML-RPC packet to a Python object. If the XML-RPC packet
1129 # @param data An XML-RPC packet, given as an 8-bit string.
1135 """data -> unmarshalled data, method name
1137 Convert an XML-RPC packet to unmarshalled data plus a method
1140 If the XML-RPC packet represents a fault condition, this function
1150 # Content-Encoding: gzip
1157 """data -> gzip encoded data
1165 gzf.write(data)
1173 # Content-Encoding: gzip
1184 """gzip encoded data -> unencoded data
1206 # Return a decoded file-like object for the gzip encoding
1210 # @return a file-like object that the decoded data can be read() from
1213 """a file-like object to decode a response encoded with the gzip
1231 # --------------------------------------------------------------------
1235 # some magic to bind an XML-RPC method to an RPC server.
1246 # Standard transport class for XML-RPC over HTTP.
1252 """Handles an HTTP transaction to an XML-RPC server."""
1273 # @param host Target host.
1275 # @param request_body XML-RPC request body.
1279 def request(self, host, handler, request_body, verbose=0): argument
1283 return self.single_request(host, handler, request_body, verbose)
1294 # @param host Target host.
1296 # @param request_body XML-RPC request body.
1300 def single_request(self, host, handler, request_body, verbose=0): argument
1301 # issue XML-RPC request
1303 h = self.make_connection(host)
1309 self.send_host(h, host)
1326 if (response.getheader("content-length", 0)):
1329 host + handler,
1337 # @return A 2-tuple containing a parser and an unmarshaller.
1344 # Get authorization info from host parameter
1345 # Host may be a string, or a (host, x509-dict) tuple; if a string,
1346 # it is checked for a "user:pw@host" format, and a "Basic
1349 # @param host Host descriptor (URL or (URL, x509 info) tuple).
1350 # @return A 3-tuple containing (actual host, extra headers,
1353 def get_host_info(self, host): argument
1356 if isinstance(host, TupleType):
1357 host, x509 = host
1360 auth, host = urllib.splituser(host)
1372 return host, extra_headers, x509
1377 # @param host Target host.
1380 def make_connection(self, host): argument
1382 #HTTP/1.1 keep-alive.
1383 if self._connection and host == self._connection[0]:
1386 # create a HTTP connection object from a host descriptor
1387 chost, self._extra_headers, x509 = self.get_host_info(host)
1388 #store the host argument along with the connection object
1389 self._connection = host, httplib.HTTPConnection(chost)
1397 host, connection = self._connection
1407 # @param request_body XML-RPC body.
1412 connection.putheader("Accept-Encoding", "gzip")
1417 # Send host name.
1420 # @param host Host name.
1422 # Note: This function doesn't actually add the "Host"
1426 def send_host(self, connection, host): argument
1435 # Send user-agent identifier.
1440 connection.putheader("User-Agent", self.user_agent)
1446 # @param request_body XML-RPC request body.
1449 connection.putheader("Content-Type", "text/xml")
1455 connection.putheader("Content-Encoding", "gzip")
1458 connection.putheader("Content-Length", str(len(request_body)))
1472 if response.getheader("Content-Encoding", "") == "gzip":
1496 # Standard transport class for XML-RPC over HTTPS.
1499 """Handles an HTTPS transaction to an XML-RPC server."""
1507 def make_connection(self, host): argument
1508 if self._connection and host == self._connection[0]:
1510 # create a HTTPS connection object from a host descriptor
1511 # host may be a string, or a (host, x509-dict) tuple
1519 chost, self._extra_headers, x509 = self.get_host_info(host)
1520 self._connection = host, HTTPS(chost, None, context=self.context, **(x509 or {}))
1525 # to an XML-RPC server.
1534 # @keyparam encoding The default encoding used for 8-bit strings
1535 # (default is UTF-8).
1541 """uri [,options] -> a logical connection to an XML-RPC server
1544 scheme://host/target.
1556 encoding: the request encoding (default is UTF-8)
1558 All 8-bit strings passed to the server proxy are assumed to use
1567 uri = uri.encode('ISO-8859-1')
1573 raise IOError, "unsupported XML-RPC protocol"
1622 # note: to call a remote object with a non-standard name, use
1623 # result getattr(server, "strange-python-name")(args)
1639 # --------------------------------------------------------------------