blob: a873f770fa19b17760513c85dea3e176ee2d0329 [file] [log] [blame]
Fredrik Lundhb9056332001-07-11 17:42:21 +00001#
2# XML-RPC CLIENT LIBRARY
3# $Id$
4#
Fredrik Lundhc4c062f2001-09-10 19:45:02 +00005# an XML-RPC client interface for Python.
6#
7# the marshalling and response parser code can also be used to
8# implement XML-RPC servers.
9#
10# Notes:
11# this version is designed to work with Python 1.5.2 or newer.
12# unicode encoding support requires at least Python 1.6.
13# experimental HTTPS requires Python 2.0 built with SSL sockets.
14# expat parser support requires Python 2.0 with pyexpat support.
15#
Fredrik Lundhb9056332001-07-11 17:42:21 +000016# History:
17# 1999-01-14 fl Created
18# 1999-01-15 fl Changed dateTime to use localtime
19# 1999-01-16 fl Added Binary/base64 element, default to RPC2 service
20# 1999-01-19 fl Fixed array data element (from Skip Montanaro)
21# 1999-01-21 fl Fixed dateTime constructor, etc.
22# 1999-02-02 fl Added fault handling, handle empty sequences, etc.
23# 1999-02-10 fl Fixed problem with empty responses (from Skip Montanaro)
24# 1999-06-20 fl Speed improvements, pluggable parsers/transports (0.9.8)
25# 2000-11-28 fl Changed boolean to check the truth value of its argument
26# 2001-02-24 fl Added encoding/Unicode/SafeTransport patches
27# 2001-02-26 fl Added compare support to wrappers (0.9.9/1.0b1)
28# 2001-03-28 fl Make sure response tuple is a singleton
29# 2001-03-29 fl Don't require empty params element (from Nicholas Riley)
Fredrik Lundh78eedce2001-08-23 20:04:33 +000030# 2001-06-10 fl Folded in _xmlrpclib accelerator support (1.0b2)
31# 2001-08-20 fl Base xmlrpclib.Error on built-in Exception (from Paul Prescod)
Fredrik Lundhc4c062f2001-09-10 19:45:02 +000032# 2001-09-03 fl Allow Transport subclass to override getparser
33# 2001-09-10 fl Lazy import of urllib, cgi, xmllib (20x import speedup)
Fredrik Lundh1538c232001-10-01 19:42:03 +000034# 2001-10-01 fl Remove containers from memo cache when done with them
35# 2001-10-01 fl Use faster escape method (80% dumps speedup)
Fredrik Lundh3d9addd2002-06-27 21:36:21 +000036# 2001-10-02 fl More dumps microtuning
37# 2001-10-04 fl Make sure import expat gets a parser (from Guido van Rossum)
Skip Montanaro5e9c71b2001-10-10 15:56:34 +000038# 2001-10-10 sm Allow long ints to be passed as ints if they don't overflow
Fredrik Lundh3d9addd2002-06-27 21:36:21 +000039# 2001-10-17 sm Test for int and long overflow (allows use on 64-bit systems)
Fredrik Lundhb6ab93f2001-12-19 21:40:04 +000040# 2001-11-12 fl Use repr() to marshal doubles (from Paul Felix)
Fredrik Lundh3d9addd2002-06-27 21:36:21 +000041# 2002-03-17 fl Avoid buffered read when possible (from James Rucker)
42# 2002-04-07 fl Added pythondoc comments
43# 2002-04-16 fl Added __str__ methods to datetime/binary wrappers
44# 2002-05-15 fl Added error constants (from Andrew Kuchling)
45# 2002-06-27 fl Merged with Python CVS version
Fredrik Lundh1303c7c2002-10-22 18:23:00 +000046# 2002-10-22 fl Added basic authentication (based on code from Phillip Eby)
Fredrik Lundhb9056332001-07-11 17:42:21 +000047#
Fredrik Lundh3d9addd2002-06-27 21:36:21 +000048# Copyright (c) 1999-2002 by Secret Labs AB.
49# Copyright (c) 1999-2002 by Fredrik Lundh.
Fredrik Lundhb9056332001-07-11 17:42:21 +000050#
51# info@pythonware.com
52# http://www.pythonware.com
53#
54# --------------------------------------------------------------------
55# The XML-RPC client interface is
56#
Fredrik Lundh3d9addd2002-06-27 21:36:21 +000057# Copyright (c) 1999-2002 by Secret Labs AB
58# Copyright (c) 1999-2002 by Fredrik Lundh
Fredrik Lundhb9056332001-07-11 17:42:21 +000059#
60# By obtaining, using, and/or copying this software and/or its
61# associated documentation, you agree that you have read, understood,
62# and will comply with the following terms and conditions:
63#
64# Permission to use, copy, modify, and distribute this software and
65# its associated documentation for any purpose and without fee is
66# hereby granted, provided that the above copyright notice appears in
67# all copies, and that both that copyright notice and this permission
68# notice appear in supporting documentation, and that the name of
69# Secret Labs AB or the author not be used in advertising or publicity
70# pertaining to distribution of the software without specific, written
71# prior permission.
72#
73# SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
74# TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-
75# ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR
76# BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
77# DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
78# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
79# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
80# OF THIS SOFTWARE.
81# --------------------------------------------------------------------
82
83#
Fredrik Lundh3d9addd2002-06-27 21:36:21 +000084# things to look into some day:
Fredrik Lundhb9056332001-07-11 17:42:21 +000085
Fredrik Lundh3d9addd2002-06-27 21:36:21 +000086# TODO: sort out True/False/boolean issues for Python 2.3
Fredrik Lundhb9056332001-07-11 17:42:21 +000087
Fred Drake1b410792001-09-04 18:55:03 +000088"""
89An XML-RPC client interface for Python.
90
91The marshalling and response parser code can also be used to
92implement XML-RPC servers.
93
Fred Drake1b410792001-09-04 18:55:03 +000094Exported exceptions:
95
Fredrik Lundhc4c062f2001-09-10 19:45:02 +000096 Error Base class for client errors
97 ProtocolError Indicates an HTTP protocol error
98 ResponseError Indicates a broken response package
99 Fault Indicates an XML-RPC fault package
Fred Drake1b410792001-09-04 18:55:03 +0000100
101Exported classes:
102
Fredrik Lundhc4c062f2001-09-10 19:45:02 +0000103 ServerProxy Represents a logical connection to an XML-RPC server
Fred Drake1b410792001-09-04 18:55:03 +0000104
Fredrik Lundhc4c062f2001-09-10 19:45:02 +0000105 Boolean boolean wrapper to generate a "boolean" XML-RPC value
106 DateTime dateTime wrapper for an ISO 8601 string or time tuple or
107 localtime integer value to generate a "dateTime.iso8601"
108 XML-RPC value
109 Binary binary data wrapper
Fred Drake1b410792001-09-04 18:55:03 +0000110
Fredrik Lundhc4c062f2001-09-10 19:45:02 +0000111 SlowParser Slow but safe standard parser (based on xmllib)
112 Marshaller Generate an XML-RPC params chunk from a Python data structure
113 Unmarshaller Unmarshal an XML-RPC response from incoming XML event message
114 Transport Handles an HTTP transaction to an XML-RPC server
115 SafeTransport Handles an HTTPS transaction to an XML-RPC server
Fred Drake1b410792001-09-04 18:55:03 +0000116
117Exported constants:
118
Fredrik Lundhc4c062f2001-09-10 19:45:02 +0000119 True
120 False
Fred Drake1b410792001-09-04 18:55:03 +0000121
122Exported functions:
123
Fredrik Lundhc4c062f2001-09-10 19:45:02 +0000124 boolean Convert any Python value to an XML-RPC boolean
125 getparser Create instance of the fastest available parser & attach
126 to an unmarshalling object
127 dumps Convert an argument tuple or a Fault instance to an XML-RPC
128 request (or response, if the methodresponse option is used).
129 loads Convert an XML-RPC packet to unmarshalled data plus a method
130 name (None if not present).
Fred Drake1b410792001-09-04 18:55:03 +0000131"""
132
Fred Drake2a2d9702001-10-17 01:51:04 +0000133import re, string, time, operator
Fredrik Lundh1538c232001-10-01 19:42:03 +0000134
Fredrik Lundhb9056332001-07-11 17:42:21 +0000135from types import *
Fredrik Lundhb9056332001-07-11 17:42:21 +0000136
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000137# --------------------------------------------------------------------
138# Internal stuff
139
Fredrik Lundhb9056332001-07-11 17:42:21 +0000140try:
141 unicode
142except NameError:
143 unicode = None # unicode support not available
144
Skip Montanaro9a7c96a2003-01-22 18:17:25 +0000145try:
146 _bool_is_builtin = False.__class__.__name__ == "bool"
147except NameError:
148 _bool_is_builtin = 0
149
Fredrik Lundhb9056332001-07-11 17:42:21 +0000150def _decode(data, encoding, is8bit=re.compile("[\x80-\xff]").search):
151 # decode non-ascii string (if possible)
152 if unicode and encoding and is8bit(data):
153 data = unicode(data, encoding)
154 return data
155
Fredrik Lundh1538c232001-10-01 19:42:03 +0000156def escape(s, replace=string.replace):
157 s = replace(s, "&", "&")
158 s = replace(s, "<", "&lt;")
159 return replace(s, ">", "&gt;",)
160
Fredrik Lundhb9056332001-07-11 17:42:21 +0000161if unicode:
162 def _stringify(string):
163 # convert to 7-bit ascii if possible
164 try:
165 return str(string)
166 except UnicodeError:
167 return string
168else:
169 def _stringify(string):
170 return string
171
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000172__version__ = "1.0.1"
173
174# xmlrpc integer limits
175MAXINT = 2L**31-1
176MININT = -2L**31
177
178# --------------------------------------------------------------------
179# Error constants (from Dan Libby's specification at
180# http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php)
181
182# Ranges of errors
183PARSE_ERROR = -32700
184SERVER_ERROR = -32600
185APPLICATION_ERROR = -32500
186SYSTEM_ERROR = -32400
187TRANSPORT_ERROR = -32300
188
189# Specific errors
190NOT_WELLFORMED_ERROR = -32700
191UNSUPPORTED_ENCODING = -32701
192INVALID_ENCODING_CHAR = -32702
193INVALID_XMLRPC = -32600
194METHOD_NOT_FOUND = -32601
195INVALID_METHOD_PARAMS = -32602
196INTERNAL_ERROR = -32603
Fredrik Lundhb9056332001-07-11 17:42:21 +0000197
198# --------------------------------------------------------------------
199# Exceptions
200
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000201##
202# Base class for all kinds of client-side errors.
203
Fredrik Lundh78eedce2001-08-23 20:04:33 +0000204class Error(Exception):
Fred Drake1b410792001-09-04 18:55:03 +0000205 """Base class for client errors."""
Fredrik Lundhb0e8e9b2001-09-10 21:45:42 +0000206 def __str__(self):
207 return repr(self)
Fredrik Lundhb9056332001-07-11 17:42:21 +0000208
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000209##
210# Indicates an HTTP-level protocol error. This is raised by the HTTP
211# transport layer, if the server returns an error code other than 200
212# (OK).
213#
214# @param url The target URL.
215# @param errcode The HTTP error code.
216# @param errmsg The HTTP error message.
217# @param headers The HTTP header dictionary.
218
Fredrik Lundhb9056332001-07-11 17:42:21 +0000219class ProtocolError(Error):
Fred Drake1b410792001-09-04 18:55:03 +0000220 """Indicates an HTTP protocol error."""
Fredrik Lundhb9056332001-07-11 17:42:21 +0000221 def __init__(self, url, errcode, errmsg, headers):
Fredrik Lundhb0e8e9b2001-09-10 21:45:42 +0000222 Error.__init__(self)
Fredrik Lundhb9056332001-07-11 17:42:21 +0000223 self.url = url
224 self.errcode = errcode
225 self.errmsg = errmsg
226 self.headers = headers
227 def __repr__(self):
228 return (
229 "<ProtocolError for %s: %s %s>" %
230 (self.url, self.errcode, self.errmsg)
231 )
232
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000233##
234# Indicates a broken XML-RPC response package. This exception is
235# raised by the unmarshalling layer, if the XML-RPC response is
236# malformed.
237
Fredrik Lundhb9056332001-07-11 17:42:21 +0000238class ResponseError(Error):
Fredrik Lundhc4c062f2001-09-10 19:45:02 +0000239 """Indicates a broken response package."""
Fredrik Lundhb9056332001-07-11 17:42:21 +0000240 pass
241
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000242##
243# Indicates an XML-RPC fault response package. This exception is
244# raised by the unmarshalling layer, if the XML-RPC response contains
245# a fault string. This exception can also used as a class, to
246# generate a fault XML-RPC message.
247#
248# @param faultCode The XML-RPC fault code.
249# @param faultString The XML-RPC fault string.
250
Fredrik Lundhb9056332001-07-11 17:42:21 +0000251class Fault(Error):
Fredrik Lundhc4c062f2001-09-10 19:45:02 +0000252 """Indicates an XML-RPC fault package."""
Fredrik Lundhb9056332001-07-11 17:42:21 +0000253 def __init__(self, faultCode, faultString, **extra):
Fredrik Lundhb0e8e9b2001-09-10 21:45:42 +0000254 Error.__init__(self)
Fredrik Lundhb9056332001-07-11 17:42:21 +0000255 self.faultCode = faultCode
256 self.faultString = faultString
257 def __repr__(self):
258 return (
259 "<Fault %s: %s>" %
Fredrik Lundhb0e8e9b2001-09-10 21:45:42 +0000260 (self.faultCode, repr(self.faultString))
Fredrik Lundhb9056332001-07-11 17:42:21 +0000261 )
262
263# --------------------------------------------------------------------
264# Special values
265
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000266##
267# Wrapper for XML-RPC boolean values. Use the xmlrpclib.True and
268# xmlrpclib.False constants, or the xmlrpclib.boolean() function, to
269# generate boolean XML-RPC values.
270#
271# @param value A boolean value. Any true value is interpreted as True,
272# all other values are interpreted as False.
273
Skip Montanaro9a7c96a2003-01-22 18:17:25 +0000274if _bool_is_builtin:
275 boolean = Boolean = bool
276 # to avoid breaking code which references xmlrpclib.{True,False}
277 True, False = True, False
278else:
279 class Boolean:
280 """Boolean-value wrapper.
Fred Drake1b410792001-09-04 18:55:03 +0000281
Skip Montanaro9a7c96a2003-01-22 18:17:25 +0000282 Use True or False to generate a "boolean" XML-RPC value.
283 """
Fredrik Lundhb9056332001-07-11 17:42:21 +0000284
Skip Montanaro9a7c96a2003-01-22 18:17:25 +0000285 def __init__(self, value = 0):
286 self.value = operator.truth(value)
Fredrik Lundhb9056332001-07-11 17:42:21 +0000287
Skip Montanaro9a7c96a2003-01-22 18:17:25 +0000288 def encode(self, out):
289 out.write("<value><boolean>%d</boolean></value>\n" % self.value)
Fredrik Lundhb9056332001-07-11 17:42:21 +0000290
Skip Montanaro9a7c96a2003-01-22 18:17:25 +0000291 def __cmp__(self, other):
292 if isinstance(other, Boolean):
293 other = other.value
294 return cmp(self.value, other)
Fredrik Lundhb9056332001-07-11 17:42:21 +0000295
Skip Montanaro9a7c96a2003-01-22 18:17:25 +0000296 def __repr__(self):
297 if self.value:
298 return "<Boolean True at %x>" % id(self)
299 else:
300 return "<Boolean False at %x>" % id(self)
Fredrik Lundhb9056332001-07-11 17:42:21 +0000301
Skip Montanaro9a7c96a2003-01-22 18:17:25 +0000302 def __int__(self):
303 return self.value
Fredrik Lundhb9056332001-07-11 17:42:21 +0000304
Skip Montanaro9a7c96a2003-01-22 18:17:25 +0000305 def __nonzero__(self):
306 return self.value
Fredrik Lundhb9056332001-07-11 17:42:21 +0000307
Skip Montanaro9a7c96a2003-01-22 18:17:25 +0000308 True, False = Boolean(1), Boolean(0)
Fredrik Lundhb9056332001-07-11 17:42:21 +0000309
Skip Montanaro9a7c96a2003-01-22 18:17:25 +0000310 ##
311 # Map true or false value to XML-RPC boolean values.
312 #
313 # @def boolean(value)
314 # @param value A boolean value. Any true value is mapped to True,
315 # all other values are mapped to False.
316 # @return xmlrpclib.True or xmlrpclib.False.
317 # @see Boolean
318 # @see True
319 # @see False
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000320
Skip Montanaro9a7c96a2003-01-22 18:17:25 +0000321 def boolean(value, _truefalse=(False, True)):
322 """Convert any Python value to XML-RPC 'boolean'."""
323 return _truefalse[operator.truth(value)]
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000324
325##
326# Wrapper for XML-RPC DateTime values. This converts a time value to
327# the format used by XML-RPC.
328# <p>
329# The value can be given as a string in the format
330# "yyyymmddThh:mm:ss", as a 9-item time tuple (as returned by
331# time.localtime()), or an integer value (as returned by time.time()).
332# The wrapper uses time.localtime() to convert an integer to a time
333# tuple.
334#
335# @param value The time, given as an ISO 8601 string, a time
336# tuple, or a integer time value.
Fredrik Lundhb9056332001-07-11 17:42:21 +0000337
Fredrik Lundhb9056332001-07-11 17:42:21 +0000338class DateTime:
Fredrik Lundhc4c062f2001-09-10 19:45:02 +0000339 """DateTime wrapper for an ISO 8601 string or time tuple or
340 localtime integer value to generate 'dateTime.iso8601' XML-RPC
341 value.
342 """
Fredrik Lundhb9056332001-07-11 17:42:21 +0000343
344 def __init__(self, value=0):
Fredrik Lundh78eedce2001-08-23 20:04:33 +0000345 if not isinstance(value, StringType):
Gustavo Niemeyerd5b80902003-06-16 02:49:42 +0000346 if not isinstance(value, (TupleType, time.struct_time)):
Fredrik Lundhb9056332001-07-11 17:42:21 +0000347 if value == 0:
348 value = time.time()
349 value = time.localtime(value)
350 value = time.strftime("%Y%m%dT%H:%M:%S", value)
351 self.value = value
352
353 def __cmp__(self, other):
354 if isinstance(other, DateTime):
355 other = other.value
356 return cmp(self.value, other)
357
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000358 ##
359 # Get date/time value.
360 #
361 # @return Date/time value, as an ISO 8601 string.
362
363 def __str__(self):
364 return self.value
365
Fredrik Lundhb9056332001-07-11 17:42:21 +0000366 def __repr__(self):
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000367 return "<DateTime %s at %x>" % (repr(self.value), id(self))
Fredrik Lundhb9056332001-07-11 17:42:21 +0000368
369 def decode(self, data):
370 self.value = string.strip(data)
371
372 def encode(self, out):
373 out.write("<value><dateTime.iso8601>")
374 out.write(self.value)
375 out.write("</dateTime.iso8601></value>\n")
376
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000377def _datetime(data):
378 # decode xml element contents into a DateTime structure.
Fredrik Lundhb9056332001-07-11 17:42:21 +0000379 value = DateTime()
380 value.decode(data)
381 return value
382
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000383##
384# Wrapper for binary data. This can be used to transport any kind
385# of binary data over XML-RPC, using BASE64 encoding.
386#
387# @param data An 8-bit string containing arbitrary data.
388
Skip Montanarobfcbfa72003-04-24 19:51:31 +0000389import base64
390try:
391 import cStringIO as StringIO
392except ImportError:
393 import StringIO
394
Fredrik Lundhb9056332001-07-11 17:42:21 +0000395class Binary:
Fred Drake1b410792001-09-04 18:55:03 +0000396 """Wrapper for binary data."""
Fredrik Lundhb9056332001-07-11 17:42:21 +0000397
398 def __init__(self, data=None):
399 self.data = data
400
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000401 ##
402 # Get buffer contents.
403 #
404 # @return Buffer contents, as an 8-bit string.
405
406 def __str__(self):
407 return self.data or ""
408
Fredrik Lundhb9056332001-07-11 17:42:21 +0000409 def __cmp__(self, other):
410 if isinstance(other, Binary):
411 other = other.data
412 return cmp(self.data, other)
413
414 def decode(self, data):
Fredrik Lundhb9056332001-07-11 17:42:21 +0000415 self.data = base64.decodestring(data)
416
417 def encode(self, out):
Fredrik Lundhb9056332001-07-11 17:42:21 +0000418 out.write("<value><base64>\n")
419 base64.encode(StringIO.StringIO(self.data), out)
420 out.write("</base64></value>\n")
421
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000422def _binary(data):
423 # decode xml element contents into a Binary structure
Fredrik Lundhb9056332001-07-11 17:42:21 +0000424 value = Binary()
425 value.decode(data)
426 return value
427
Skip Montanaro9a7c96a2003-01-22 18:17:25 +0000428WRAPPERS = (DateTime, Binary)
429if not _bool_is_builtin:
430 WRAPPERS = WRAPPERS + (Boolean,)
Fredrik Lundhb9056332001-07-11 17:42:21 +0000431
432# --------------------------------------------------------------------
433# XML parsers
434
435try:
436 # optional xmlrpclib accelerator. for more information on this
437 # component, contact info@pythonware.com
438 import _xmlrpclib
439 FastParser = _xmlrpclib.Parser
440 FastUnmarshaller = _xmlrpclib.Unmarshaller
441except (AttributeError, ImportError):
442 FastParser = FastUnmarshaller = None
443
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000444try:
445 import _xmlrpclib
446 FastMarshaller = _xmlrpclib.Marshaller
447except (AttributeError, ImportError):
448 FastMarshaller = None
449
Fredrik Lundhb9056332001-07-11 17:42:21 +0000450#
451# the SGMLOP parser is about 15x faster than Python's builtin
452# XML parser. SGMLOP sources can be downloaded from:
453#
454# http://www.pythonware.com/products/xml/sgmlop.htm
455#
456
457try:
458 import sgmlop
459 if not hasattr(sgmlop, "XMLParser"):
460 raise ImportError
461except ImportError:
462 SgmlopParser = None # sgmlop accelerator not available
463else:
464 class SgmlopParser:
465 def __init__(self, target):
466
467 # setup callbacks
468 self.finish_starttag = target.start
469 self.finish_endtag = target.end
470 self.handle_data = target.data
471 self.handle_xml = target.xml
472
473 # activate parser
474 self.parser = sgmlop.XMLParser()
475 self.parser.register(self)
476 self.feed = self.parser.feed
477 self.entity = {
478 "amp": "&", "gt": ">", "lt": "<",
479 "apos": "'", "quot": '"'
480 }
481
482 def close(self):
483 try:
484 self.parser.close()
485 finally:
486 self.parser = self.feed = None # nuke circular reference
487
488 def handle_proc(self, tag, attr):
489 m = re.search("encoding\s*=\s*['\"]([^\"']+)[\"']", attr)
490 if m:
491 self.handle_xml(m.group(1), 1)
492
493 def handle_entityref(self, entity):
494 # <string> entity
495 try:
496 self.handle_data(self.entity[entity])
497 except KeyError:
498 self.handle_data("&%s;" % entity)
499
500try:
501 from xml.parsers import expat
Guido van Rossumb8551342001-10-02 18:33:11 +0000502 if not hasattr(expat, "ParserCreate"):
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000503 raise ImportError
Fredrik Lundhb9056332001-07-11 17:42:21 +0000504except ImportError:
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000505 ExpatParser = None # expat not available
Fredrik Lundhb9056332001-07-11 17:42:21 +0000506else:
507 class ExpatParser:
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000508 # fast expat parser for Python 2.0 and later. this is about
509 # 50% slower than sgmlop, on roundtrip testing
Fredrik Lundhb9056332001-07-11 17:42:21 +0000510 def __init__(self, target):
511 self._parser = parser = expat.ParserCreate(None, None)
512 self._target = target
513 parser.StartElementHandler = target.start
514 parser.EndElementHandler = target.end
515 parser.CharacterDataHandler = target.data
516 encoding = None
517 if not parser.returns_unicode:
518 encoding = "utf-8"
519 target.xml(encoding, None)
520
521 def feed(self, data):
522 self._parser.Parse(data, 0)
523
524 def close(self):
525 self._parser.Parse("", 1) # end of data
526 del self._target, self._parser # get rid of circular references
527
Fredrik Lundhc4c062f2001-09-10 19:45:02 +0000528class SlowParser:
529 """Default XML parser (based on xmllib.XMLParser)."""
530 # this is about 10 times slower than sgmlop, on roundtrip
531 # testing.
Fredrik Lundhb9056332001-07-11 17:42:21 +0000532 def __init__(self, target):
Fredrik Lundhc4c062f2001-09-10 19:45:02 +0000533 import xmllib # lazy subclassing (!)
534 if xmllib.XMLParser not in SlowParser.__bases__:
535 SlowParser.__bases__ = (xmllib.XMLParser,)
Fredrik Lundhb9056332001-07-11 17:42:21 +0000536 self.handle_xml = target.xml
537 self.unknown_starttag = target.start
538 self.handle_data = target.data
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000539 self.handle_cdata = target.data
Fredrik Lundhb9056332001-07-11 17:42:21 +0000540 self.unknown_endtag = target.end
Fredrik Lundhb0e8e9b2001-09-10 21:45:42 +0000541 try:
542 xmllib.XMLParser.__init__(self, accept_utf8=1)
543 except TypeError:
544 xmllib.XMLParser.__init__(self) # pre-2.0
Fredrik Lundhb9056332001-07-11 17:42:21 +0000545
546# --------------------------------------------------------------------
547# XML-RPC marshalling and unmarshalling code
548
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000549##
550# XML-RPC marshaller.
551#
552# @param encoding Default encoding for 8-bit strings. The default
553# value is None (interpreted as UTF-8).
554# @see dumps
555
Fredrik Lundhb9056332001-07-11 17:42:21 +0000556class Marshaller:
Fred Drake1b410792001-09-04 18:55:03 +0000557 """Generate an XML-RPC params chunk from a Python data structure.
Fredrik Lundhb9056332001-07-11 17:42:21 +0000558
Fredrik Lundhc4c062f2001-09-10 19:45:02 +0000559 Create a Marshaller instance for each set of parameters, and use
560 the "dumps" method to convert your data (represented as a tuple)
561 to an XML-RPC params chunk. To write a fault response, pass a
562 Fault instance instead. You may prefer to use the "dumps" module
563 function for this purpose.
Fred Drake1b410792001-09-04 18:55:03 +0000564 """
Fredrik Lundhb9056332001-07-11 17:42:21 +0000565
566 # by the way, if you don't understand what's going on in here,
567 # that's perfectly ok.
568
Andrew M. Kuchlinga4c2b742003-04-25 00:26:51 +0000569 def __init__(self, encoding=None, allow_none=0):
Fredrik Lundhb9056332001-07-11 17:42:21 +0000570 self.memo = {}
571 self.data = None
572 self.encoding = encoding
Andrew M. Kuchlinga4c2b742003-04-25 00:26:51 +0000573 self.allow_none = allow_none
Tim Petersc2659cf2003-05-12 20:19:37 +0000574
Fredrik Lundhb9056332001-07-11 17:42:21 +0000575 dispatch = {}
576
577 def dumps(self, values):
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000578 out = []
579 write = out.append
580 dump = self.__dump
Fredrik Lundhb9056332001-07-11 17:42:21 +0000581 if isinstance(values, Fault):
582 # fault instance
583 write("<fault>\n")
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000584 dump(vars(values), write)
Fredrik Lundhb9056332001-07-11 17:42:21 +0000585 write("</fault>\n")
586 else:
587 # parameter block
Fredrik Lundhc266bb02001-08-23 20:13:08 +0000588 # FIXME: the xml-rpc specification allows us to leave out
589 # the entire <params> block if there are no parameters.
590 # however, changing this may break older code (including
591 # old versions of xmlrpclib.py), so this is better left as
592 # is for now. See @XMLRPC3 for more information. /F
Fredrik Lundhb9056332001-07-11 17:42:21 +0000593 write("<params>\n")
594 for v in values:
595 write("<param>\n")
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000596 dump(v, write)
Fredrik Lundhb9056332001-07-11 17:42:21 +0000597 write("</param>\n")
598 write("</params>\n")
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000599 result = string.join(out, "")
Fredrik Lundhb9056332001-07-11 17:42:21 +0000600 return result
601
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000602 def __dump(self, value, write):
Fredrik Lundhb9056332001-07-11 17:42:21 +0000603 try:
604 f = self.dispatch[type(value)]
605 except KeyError:
606 raise TypeError, "cannot marshal %s objects" % type(value)
607 else:
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000608 f(self, value, write)
Fredrik Lundhb9056332001-07-11 17:42:21 +0000609
Andrew M. Kuchlinga4c2b742003-04-25 00:26:51 +0000610 def dump_nil (self, value, write):
611 if not self.allow_none:
612 raise TypeError, "cannot marshal None unless allow_none is enabled"
613 write("<value><nil/></value>")
614 dispatch[NoneType] = dump_nil
Tim Petersc2659cf2003-05-12 20:19:37 +0000615
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000616 def dump_int(self, value, write):
Skip Montanaro5449e082001-10-17 22:53:33 +0000617 # in case ints are > 32 bits
618 if value > MAXINT or value < MININT:
619 raise OverflowError, "int exceeds XML-RPC limits"
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000620 write("<value><int>")
621 write(str(value))
622 write("</int></value>\n")
Fredrik Lundhb9056332001-07-11 17:42:21 +0000623 dispatch[IntType] = dump_int
624
Skip Montanaro9a7c96a2003-01-22 18:17:25 +0000625 if _bool_is_builtin:
626 def dump_bool(self, value, write):
627 write("<value><boolean>")
628 write(value and "1" or "0")
629 write("</boolean></value>\n")
630 dispatch[bool] = dump_bool
631
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000632 def dump_long(self, value, write):
Skip Montanaro5449e082001-10-17 22:53:33 +0000633 if value > MAXINT or value < MININT:
634 raise OverflowError, "long int exceeds XML-RPC limits"
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000635 write("<value><int>")
636 write(str(int(value)))
637 write("</int></value>\n")
Skip Montanaro5e9c71b2001-10-10 15:56:34 +0000638 dispatch[LongType] = dump_long
639
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000640 def dump_double(self, value, write):
641 write("<value><double>")
642 write(repr(value))
643 write("</double></value>\n")
Fredrik Lundhb9056332001-07-11 17:42:21 +0000644 dispatch[FloatType] = dump_double
645
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000646 def dump_string(self, value, write, escape=escape):
647 write("<value><string>")
648 write(escape(value))
649 write("</string></value>\n")
Fredrik Lundhb9056332001-07-11 17:42:21 +0000650 dispatch[StringType] = dump_string
651
652 if unicode:
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000653 def dump_unicode(self, value, write, escape=escape):
Fredrik Lundhb9056332001-07-11 17:42:21 +0000654 value = value.encode(self.encoding)
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000655 write("<value><string>")
656 write(escape(value))
657 write("</string></value>\n")
Fredrik Lundhb9056332001-07-11 17:42:21 +0000658 dispatch[UnicodeType] = dump_unicode
659
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000660 def dump_array(self, value, write):
661 i = id(value)
662 if self.memo.has_key(i):
663 raise TypeError, "cannot marshal recursive sequences"
664 self.memo[i] = None
Fredrik Lundh1538c232001-10-01 19:42:03 +0000665 dump = self.__dump
Fredrik Lundhb9056332001-07-11 17:42:21 +0000666 write("<value><array><data>\n")
667 for v in value:
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000668 dump(v, write)
Fredrik Lundhb9056332001-07-11 17:42:21 +0000669 write("</data></array></value>\n")
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000670 del self.memo[i]
Fredrik Lundhb9056332001-07-11 17:42:21 +0000671 dispatch[TupleType] = dump_array
672 dispatch[ListType] = dump_array
673
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000674 def dump_struct(self, value, write, escape=escape):
675 i = id(value)
676 if self.memo.has_key(i):
677 raise TypeError, "cannot marshal recursive dictionaries"
678 self.memo[i] = None
Fredrik Lundh1538c232001-10-01 19:42:03 +0000679 dump = self.__dump
Fredrik Lundhb9056332001-07-11 17:42:21 +0000680 write("<value><struct>\n")
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000681 for k in value.keys():
Fredrik Lundhb9056332001-07-11 17:42:21 +0000682 write("<member>\n")
683 if type(k) is not StringType:
684 raise TypeError, "dictionary key must be string"
Fredrik Lundh1538c232001-10-01 19:42:03 +0000685 write("<name>%s</name>\n" % escape(k))
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000686 dump(value[k], write)
Fredrik Lundhb9056332001-07-11 17:42:21 +0000687 write("</member>\n")
688 write("</struct></value>\n")
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000689 del self.memo[i]
Fredrik Lundhb9056332001-07-11 17:42:21 +0000690 dispatch[DictType] = dump_struct
691
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000692 def dump_instance(self, value, write):
Fredrik Lundhb9056332001-07-11 17:42:21 +0000693 # check for special wrappers
694 if value.__class__ in WRAPPERS:
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000695 self.write = write
Fredrik Lundhb9056332001-07-11 17:42:21 +0000696 value.encode(self)
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000697 del self.write
Fredrik Lundhb9056332001-07-11 17:42:21 +0000698 else:
699 # store instance attributes as a struct (really?)
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000700 self.dump_struct(value.__dict__, write)
Fredrik Lundhb9056332001-07-11 17:42:21 +0000701 dispatch[InstanceType] = dump_instance
702
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000703##
704# XML-RPC unmarshaller.
705#
706# @see loads
707
Fredrik Lundhb9056332001-07-11 17:42:21 +0000708class Unmarshaller:
Fred Drake1b410792001-09-04 18:55:03 +0000709 """Unmarshal an XML-RPC response, based on incoming XML event
Fredrik Lundhb0e8e9b2001-09-10 21:45:42 +0000710 messages (start, data, end). Call close() to get the resulting
Fred Drake1b410792001-09-04 18:55:03 +0000711 data structure.
Fredrik Lundhb9056332001-07-11 17:42:21 +0000712
Fredrik Lundhb0e8e9b2001-09-10 21:45:42 +0000713 Note that this reader is fairly tolerant, and gladly accepts bogus
714 XML-RPC data without complaining (but not bogus XML).
Fred Drake1b410792001-09-04 18:55:03 +0000715 """
Fredrik Lundhb9056332001-07-11 17:42:21 +0000716
717 # and again, if you don't understand what's going on in here,
718 # that's perfectly ok.
719
720 def __init__(self):
721 self._type = None
722 self._stack = []
723 self._marks = []
724 self._data = []
725 self._methodname = None
726 self._encoding = "utf-8"
727 self.append = self._stack.append
728
729 def close(self):
730 # return response tuple and target method
731 if self._type is None or self._marks:
732 raise ResponseError()
733 if self._type == "fault":
Guido van Rossum68468eb2003-02-27 20:14:51 +0000734 raise Fault(**self._stack[0])
Fredrik Lundhb9056332001-07-11 17:42:21 +0000735 return tuple(self._stack)
736
737 def getmethodname(self):
738 return self._methodname
739
740 #
741 # event handlers
742
743 def xml(self, encoding, standalone):
744 self._encoding = encoding
745 # FIXME: assert standalone == 1 ???
746
747 def start(self, tag, attrs):
748 # prepare to handle this element
749 if tag == "array" or tag == "struct":
750 self._marks.append(len(self._stack))
751 self._data = []
752 self._value = (tag == "value")
753
754 def data(self, text):
755 self._data.append(text)
756
Fredrik Lundh1538c232001-10-01 19:42:03 +0000757 def end(self, tag, join=string.join):
Fredrik Lundhb9056332001-07-11 17:42:21 +0000758 # call the appropriate end tag handler
759 try:
760 f = self.dispatch[tag]
761 except KeyError:
762 pass # unknown tag ?
763 else:
Fredrik Lundh1538c232001-10-01 19:42:03 +0000764 return f(self, join(self._data, ""))
Fredrik Lundhb9056332001-07-11 17:42:21 +0000765
766 #
767 # accelerator support
768
769 def end_dispatch(self, tag, data):
770 # dispatch data
771 try:
772 f = self.dispatch[tag]
773 except KeyError:
774 pass # unknown tag ?
775 else:
776 return f(self, data)
777
778 #
779 # element decoders
780
781 dispatch = {}
782
Andrew M. Kuchlinga4c2b742003-04-25 00:26:51 +0000783 def end_nil (self, data):
784 self.append(None)
785 self._value = 0
786 dispatch["nil"] = end_nil
Tim Petersc2659cf2003-05-12 20:19:37 +0000787
Fredrik Lundh1538c232001-10-01 19:42:03 +0000788 def end_boolean(self, data):
Fredrik Lundhb9056332001-07-11 17:42:21 +0000789 if data == "0":
790 self.append(False)
791 elif data == "1":
792 self.append(True)
793 else:
794 raise TypeError, "bad boolean value"
795 self._value = 0
796 dispatch["boolean"] = end_boolean
797
Fredrik Lundh1538c232001-10-01 19:42:03 +0000798 def end_int(self, data):
799 self.append(int(data))
Fredrik Lundhb9056332001-07-11 17:42:21 +0000800 self._value = 0
801 dispatch["i4"] = end_int
802 dispatch["int"] = end_int
803
Fredrik Lundh1538c232001-10-01 19:42:03 +0000804 def end_double(self, data):
805 self.append(float(data))
Fredrik Lundhb9056332001-07-11 17:42:21 +0000806 self._value = 0
807 dispatch["double"] = end_double
808
Fredrik Lundh1538c232001-10-01 19:42:03 +0000809 def end_string(self, data):
Fredrik Lundhb9056332001-07-11 17:42:21 +0000810 if self._encoding:
811 data = _decode(data, self._encoding)
812 self.append(_stringify(data))
813 self._value = 0
814 dispatch["string"] = end_string
815 dispatch["name"] = end_string # struct keys are always strings
816
817 def end_array(self, data):
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +0000818 mark = self._marks.pop()
Fredrik Lundhb9056332001-07-11 17:42:21 +0000819 # map arrays to Python lists
820 self._stack[mark:] = [self._stack[mark:]]
821 self._value = 0
822 dispatch["array"] = end_array
823
824 def end_struct(self, data):
Raymond Hettinger46ac8eb2002-06-30 03:39:14 +0000825 mark = self._marks.pop()
Fredrik Lundhb9056332001-07-11 17:42:21 +0000826 # map structs to Python dictionaries
827 dict = {}
828 items = self._stack[mark:]
829 for i in range(0, len(items), 2):
830 dict[_stringify(items[i])] = items[i+1]
831 self._stack[mark:] = [dict]
832 self._value = 0
833 dispatch["struct"] = end_struct
834
Fredrik Lundh1538c232001-10-01 19:42:03 +0000835 def end_base64(self, data):
Fredrik Lundhb9056332001-07-11 17:42:21 +0000836 value = Binary()
Fredrik Lundh1538c232001-10-01 19:42:03 +0000837 value.decode(data)
Fredrik Lundhb9056332001-07-11 17:42:21 +0000838 self.append(value)
839 self._value = 0
840 dispatch["base64"] = end_base64
841
Fredrik Lundh1538c232001-10-01 19:42:03 +0000842 def end_dateTime(self, data):
Fredrik Lundhb9056332001-07-11 17:42:21 +0000843 value = DateTime()
Fredrik Lundh1538c232001-10-01 19:42:03 +0000844 value.decode(data)
Fredrik Lundhb9056332001-07-11 17:42:21 +0000845 self.append(value)
846 dispatch["dateTime.iso8601"] = end_dateTime
847
848 def end_value(self, data):
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000849 # if we stumble upon a value element with no internal
Fredrik Lundhb9056332001-07-11 17:42:21 +0000850 # elements, treat it as a string element
851 if self._value:
852 self.end_string(data)
853 dispatch["value"] = end_value
854
855 def end_params(self, data):
856 self._type = "params"
857 dispatch["params"] = end_params
858
859 def end_fault(self, data):
860 self._type = "fault"
861 dispatch["fault"] = end_fault
862
Fredrik Lundh1538c232001-10-01 19:42:03 +0000863 def end_methodName(self, data):
Fredrik Lundhb9056332001-07-11 17:42:21 +0000864 if self._encoding:
865 data = _decode(data, self._encoding)
866 self._methodname = data
867 self._type = "methodName" # no params
868 dispatch["methodName"] = end_methodName
869
870
871# --------------------------------------------------------------------
872# convenience functions
873
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000874##
875# Create a parser object, and connect it to an unmarshalling instance.
876# This function picks the fastest available XML parser.
877#
878# return A (parser, unmarshaller) tuple.
879
Fredrik Lundhb9056332001-07-11 17:42:21 +0000880def getparser():
881 """getparser() -> parser, unmarshaller
882
Fredrik Lundhb0e8e9b2001-09-10 21:45:42 +0000883 Create an instance of the fastest available parser, and attach it
884 to an unmarshalling object. Return both objects.
Fredrik Lundhb9056332001-07-11 17:42:21 +0000885 """
886 if FastParser and FastUnmarshaller:
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000887 target = FastUnmarshaller(True, False, _binary, _datetime, Fault)
Fredrik Lundhb9056332001-07-11 17:42:21 +0000888 parser = FastParser(target)
889 else:
890 target = Unmarshaller()
891 if FastParser:
892 parser = FastParser(target)
893 elif SgmlopParser:
894 parser = SgmlopParser(target)
895 elif ExpatParser:
896 parser = ExpatParser(target)
897 else:
898 parser = SlowParser(target)
899 return parser, target
900
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000901##
902# Convert a Python tuple or a Fault instance to an XML-RPC packet.
903#
904# @def dumps(params, **options)
905# @param params A tuple or Fault instance.
906# @keyparam methodname If given, create a methodCall request for
907# this method name.
908# @keyparam methodresponse If given, create a methodResponse packet.
909# If used with a tuple, the tuple must be a singleton (that is,
910# it must contain exactly one element).
911# @keyparam encoding The packet encoding.
912# @return A string containing marshalled data.
913
Andrew M. Kuchlinga4c2b742003-04-25 00:26:51 +0000914def dumps(params, methodname=None, methodresponse=None, encoding=None,
915 allow_none=0):
Fredrik Lundhb9056332001-07-11 17:42:21 +0000916 """data [,options] -> marshalled data
917
918 Convert an argument tuple or a Fault instance to an XML-RPC
919 request (or response, if the methodresponse option is used).
920
Fredrik Lundhb0e8e9b2001-09-10 21:45:42 +0000921 In addition to the data object, the following options can be given
922 as keyword arguments:
Fredrik Lundhb9056332001-07-11 17:42:21 +0000923
924 methodname: the method name for a methodCall packet
925
926 methodresponse: true to create a methodResponse packet.
927 If this option is used with a tuple, the tuple must be
928 a singleton (i.e. it can contain only one element).
929
930 encoding: the packet encoding (default is UTF-8)
931
932 All 8-bit strings in the data structure are assumed to use the
933 packet encoding. Unicode strings are automatically converted,
Fredrik Lundhb0e8e9b2001-09-10 21:45:42 +0000934 where necessary.
Fredrik Lundhb9056332001-07-11 17:42:21 +0000935 """
936
937 assert isinstance(params, TupleType) or isinstance(params, Fault),\
938 "argument must be tuple or Fault instance"
939
940 if isinstance(params, Fault):
941 methodresponse = 1
942 elif methodresponse and isinstance(params, TupleType):
943 assert len(params) == 1, "response tuple must be a singleton"
944
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000945 if not encoding:
Fredrik Lundhb9056332001-07-11 17:42:21 +0000946 encoding = "utf-8"
947
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000948 if FastMarshaller:
949 m = FastMarshaller(encoding)
950 else:
Andrew M. Kuchlinga4c2b742003-04-25 00:26:51 +0000951 m = Marshaller(encoding, allow_none)
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000952
Fredrik Lundhb9056332001-07-11 17:42:21 +0000953 data = m.dumps(params)
954
955 if encoding != "utf-8":
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000956 xmlheader = "<?xml version='1.0' encoding='%s'?>\n" % str(encoding)
Fredrik Lundhb9056332001-07-11 17:42:21 +0000957 else:
958 xmlheader = "<?xml version='1.0'?>\n" # utf-8 is default
959
960 # standard XML-RPC wrappings
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000961 if methodname:
Fredrik Lundhb9056332001-07-11 17:42:21 +0000962 # a method call
963 if not isinstance(methodname, StringType):
964 methodname = methodname.encode(encoding)
965 data = (
966 xmlheader,
967 "<methodCall>\n"
968 "<methodName>", methodname, "</methodName>\n",
969 data,
970 "</methodCall>\n"
971 )
972 elif methodresponse:
973 # a method response, or a fault structure
974 data = (
975 xmlheader,
976 "<methodResponse>\n",
977 data,
978 "</methodResponse>\n"
979 )
980 else:
981 return data # return as is
982 return string.join(data, "")
983
Fredrik Lundh3d9addd2002-06-27 21:36:21 +0000984##
985# Convert an XML-RPC packet to a Python object. If the XML-RPC packet
986# represents a fault condition, this function raises a Fault exception.
987#
988# @param data An XML-RPC packet, given as an 8-bit string.
989# @return A tuple containing the the unpacked data, and the method name
990# (None if not present).
991# @see Fault
992
Fredrik Lundhb9056332001-07-11 17:42:21 +0000993def loads(data):
994 """data -> unmarshalled data, method name
995
996 Convert an XML-RPC packet to unmarshalled data plus a method
997 name (None if not present).
998
999 If the XML-RPC packet represents a fault condition, this function
1000 raises a Fault exception.
1001 """
Fredrik Lundh3d9addd2002-06-27 21:36:21 +00001002 import sys
Fredrik Lundhb9056332001-07-11 17:42:21 +00001003 p, u = getparser()
1004 p.feed(data)
1005 p.close()
1006 return u.close(), u.getmethodname()
1007
1008
1009# --------------------------------------------------------------------
1010# request dispatcher
1011
1012class _Method:
1013 # some magic to bind an XML-RPC method to an RPC server.
1014 # supports "nested" methods (e.g. examples.getStateName)
1015 def __init__(self, send, name):
1016 self.__send = send
1017 self.__name = name
1018 def __getattr__(self, name):
1019 return _Method(self.__send, "%s.%s" % (self.__name, name))
1020 def __call__(self, *args):
1021 return self.__send(self.__name, args)
1022
Fredrik Lundh3d9addd2002-06-27 21:36:21 +00001023##
1024# Standard transport class for XML-RPC over HTTP.
1025# <p>
1026# You can create custom transports by subclassing this method, and
1027# overriding selected methods.
Fredrik Lundhb9056332001-07-11 17:42:21 +00001028
1029class Transport:
Fredrik Lundhc4c062f2001-09-10 19:45:02 +00001030 """Handles an HTTP transaction to an XML-RPC server."""
Fredrik Lundhb9056332001-07-11 17:42:21 +00001031
1032 # client identifier (may be overridden)
1033 user_agent = "xmlrpclib.py/%s (by www.pythonware.com)" % __version__
1034
Fredrik Lundh3d9addd2002-06-27 21:36:21 +00001035 ##
1036 # Send a complete request, and parse the response.
1037 #
1038 # @param host Target host.
1039 # @param handler Target PRC handler.
1040 # @param request_body XML-RPC request body.
1041 # @param verbose Debugging flag.
1042 # @return Parsed response.
1043
Fredrik Lundhb9056332001-07-11 17:42:21 +00001044 def request(self, host, handler, request_body, verbose=0):
1045 # issue XML-RPC request
1046
1047 h = self.make_connection(host)
1048 if verbose:
1049 h.set_debuglevel(1)
1050
1051 self.send_request(h, handler, request_body)
1052 self.send_host(h, host)
1053 self.send_user_agent(h)
1054 self.send_content(h, request_body)
1055
1056 errcode, errmsg, headers = h.getreply()
1057
1058 if errcode != 200:
1059 raise ProtocolError(
1060 host + handler,
1061 errcode, errmsg,
1062 headers
1063 )
1064
1065 self.verbose = verbose
1066
Fredrik Lundh3d9addd2002-06-27 21:36:21 +00001067 try:
1068 sock = h._conn.sock
1069 except AttributeError:
1070 sock = None
1071
1072 return self._parse_response(h.getfile(), sock)
1073
1074 ##
1075 # Create parser.
1076 #
1077 # @return A 2-tuple containing a parser and a unmarshaller.
Fredrik Lundhb9056332001-07-11 17:42:21 +00001078
Fredrik Lundhc4c062f2001-09-10 19:45:02 +00001079 def getparser(self):
1080 # get parser and unmarshaller
1081 return getparser()
1082
Fredrik Lundh3d9addd2002-06-27 21:36:21 +00001083 ##
Fredrik Lundh1303c7c2002-10-22 18:23:00 +00001084 # Get authorization info from host parameter
1085 # Host may be a string, or a (host, x509-dict) tuple; if a string,
1086 # it is checked for a "user:pw@host" format, and a "Basic
1087 # Authentication" header is added if appropriate.
1088 #
1089 # @param host Host descriptor (URL or (URL, x509 info) tuple).
1090 # @return A 3-tuple containing (actual host, extra headers,
1091 # x509 info). The header and x509 fields may be None.
1092
1093 def get_host_info(self, host):
1094
1095 x509 = {}
1096 if isinstance(host, TupleType):
1097 host, x509 = host
1098
1099 import urllib
1100 auth, host = urllib.splituser(host)
1101
1102 if auth:
1103 import base64
Fredrik Lundh768c98b2002-11-01 17:14:16 +00001104 auth = base64.encodestring(urllib.unquote(auth))
Fredrik Lundh1303c7c2002-10-22 18:23:00 +00001105 auth = string.join(string.split(auth), "") # get rid of whitespace
1106 extra_headers = [
1107 ("Authorization", "Basic " + auth)
1108 ]
1109 else:
1110 extra_headers = None
1111
1112 return host, extra_headers, x509
1113
1114 ##
Fredrik Lundh3d9addd2002-06-27 21:36:21 +00001115 # Connect to server.
1116 #
1117 # @param host Target host.
1118 # @return A connection handle.
1119
Fredrik Lundhb9056332001-07-11 17:42:21 +00001120 def make_connection(self, host):
1121 # create a HTTP connection object from a host descriptor
1122 import httplib
Fredrik Lundh1303c7c2002-10-22 18:23:00 +00001123 host, extra_headers, x509 = self.get_host_info(host)
Fredrik Lundhb9056332001-07-11 17:42:21 +00001124 return httplib.HTTP(host)
1125
Fredrik Lundh3d9addd2002-06-27 21:36:21 +00001126 ##
1127 # Send request header.
1128 #
1129 # @param connection Connection handle.
1130 # @param handler Target RPC handler.
1131 # @param request_body XML-RPC body.
1132
Fredrik Lundhb9056332001-07-11 17:42:21 +00001133 def send_request(self, connection, handler, request_body):
1134 connection.putrequest("POST", handler)
1135
Fredrik Lundh3d9addd2002-06-27 21:36:21 +00001136 ##
1137 # Send host name.
1138 #
1139 # @param connection Connection handle.
1140 # @param host Host name.
1141
Fredrik Lundhb9056332001-07-11 17:42:21 +00001142 def send_host(self, connection, host):
Fredrik Lundh1303c7c2002-10-22 18:23:00 +00001143 host, extra_headers, x509 = self.get_host_info(host)
Fredrik Lundhb9056332001-07-11 17:42:21 +00001144 connection.putheader("Host", host)
Fredrik Lundh1303c7c2002-10-22 18:23:00 +00001145 if extra_headers:
1146 if isinstance(extra_headers, DictType):
1147 extra_headers = extra_headers.items()
1148 for key, value in extra_headers:
1149 connection.putheader(key, value)
Fredrik Lundhb9056332001-07-11 17:42:21 +00001150
Fredrik Lundh3d9addd2002-06-27 21:36:21 +00001151 ##
1152 # Send user-agent identifier.
1153 #
1154 # @param connection Connection handle.
1155
Fredrik Lundhb9056332001-07-11 17:42:21 +00001156 def send_user_agent(self, connection):
1157 connection.putheader("User-Agent", self.user_agent)
1158
Fredrik Lundh3d9addd2002-06-27 21:36:21 +00001159 ##
1160 # Send request body.
1161 #
1162 # @param connection Connection handle.
1163 # @param request_body XML-RPC request body.
1164
Fredrik Lundhb9056332001-07-11 17:42:21 +00001165 def send_content(self, connection, request_body):
1166 connection.putheader("Content-Type", "text/xml")
1167 connection.putheader("Content-Length", str(len(request_body)))
1168 connection.endheaders()
1169 if request_body:
1170 connection.send(request_body)
1171
Fredrik Lundh3d9addd2002-06-27 21:36:21 +00001172 ##
1173 # Parse response.
1174 #
1175 # @param file Stream.
1176 # @return Response tuple and target method.
1177
1178 def parse_response(self, file):
1179 # compatibility interface
1180 return self._parse_response(file, None)
1181
1182 ##
1183 # Parse response (alternate interface). This is similar to the
1184 # parse_response method, but also provides direct access to the
1185 # underlying socket object (where available).
1186 #
1187 # @param file Stream.
1188 # @param sock Socket handle (or None, if the socket object
1189 # could not be accessed).
1190 # @return Response tuple and target method.
1191
1192 def _parse_response(self, file, sock):
1193 # read response from input file/socket, and parse it
Fredrik Lundhb9056332001-07-11 17:42:21 +00001194
Fredrik Lundhc4c062f2001-09-10 19:45:02 +00001195 p, u = self.getparser()
Fredrik Lundhb9056332001-07-11 17:42:21 +00001196
1197 while 1:
Fredrik Lundh3d9addd2002-06-27 21:36:21 +00001198 if sock:
1199 response = sock.recv(1024)
1200 else:
1201 response = file.read(1024)
Fredrik Lundhb9056332001-07-11 17:42:21 +00001202 if not response:
1203 break
1204 if self.verbose:
1205 print "body:", repr(response)
1206 p.feed(response)
1207
Fredrik Lundh3d9addd2002-06-27 21:36:21 +00001208 file.close()
Fredrik Lundhb9056332001-07-11 17:42:21 +00001209 p.close()
1210
1211 return u.close()
1212
Fredrik Lundh3d9addd2002-06-27 21:36:21 +00001213##
1214# Standard transport class for XML-RPC over HTTPS.
1215
Fredrik Lundhb9056332001-07-11 17:42:21 +00001216class SafeTransport(Transport):
Fredrik Lundhc4c062f2001-09-10 19:45:02 +00001217 """Handles an HTTPS transaction to an XML-RPC server."""
Fredrik Lundhb9056332001-07-11 17:42:21 +00001218
Fredrik Lundh3d9addd2002-06-27 21:36:21 +00001219 # FIXME: mostly untested
1220
Fredrik Lundhb9056332001-07-11 17:42:21 +00001221 def make_connection(self, host):
1222 # create a HTTPS connection object from a host descriptor
1223 # host may be a string, or a (host, x509-dict) tuple
1224 import httplib
Fredrik Lundh1303c7c2002-10-22 18:23:00 +00001225 host, extra_headers, x509 = self.get_host_info(host)
Fredrik Lundhb9056332001-07-11 17:42:21 +00001226 try:
1227 HTTPS = httplib.HTTPS
1228 except AttributeError:
Fredrik Lundh1303c7c2002-10-22 18:23:00 +00001229 raise NotImplementedError(
1230 "your version of httplib doesn't support HTTPS"
1231 )
Fredrik Lundhb9056332001-07-11 17:42:21 +00001232 else:
Guido van Rossum68468eb2003-02-27 20:14:51 +00001233 return HTTPS(host, None, **(x509 or {}))
Fredrik Lundhb9056332001-07-11 17:42:21 +00001234
Fredrik Lundh3d9addd2002-06-27 21:36:21 +00001235##
1236# Standard server proxy. This class establishes a virtual connection
1237# to an XML-RPC server.
1238# <p>
1239# This class is available as ServerProxy and Server. New code should
1240# use ServerProxy, to avoid confusion.
1241#
1242# @def ServerProxy(uri, **options)
1243# @param uri The connection point on the server.
1244# @keyparam transport A transport factory, compatible with the
1245# standard transport class.
1246# @keyparam encoding The default encoding used for 8-bit strings
1247# (default is UTF-8).
1248# @keyparam verbose Use a true value to enable debugging output.
1249# (printed to standard output).
1250# @see Transport
1251
Fredrik Lundhb9056332001-07-11 17:42:21 +00001252class ServerProxy:
1253 """uri [,options] -> a logical connection to an XML-RPC server
1254
1255 uri is the connection point on the server, given as
1256 scheme://host/target.
1257
1258 The standard implementation always supports the "http" scheme. If
1259 SSL socket support is available (Python 2.0), it also supports
1260 "https".
1261
1262 If the target part and the slash preceding it are both omitted,
1263 "/RPC2" is assumed.
1264
1265 The following options can be given as keyword arguments:
1266
1267 transport: a transport factory
1268 encoding: the request encoding (default is UTF-8)
1269
1270 All 8-bit strings passed to the server proxy are assumed to use
1271 the given encoding.
1272 """
1273
Andrew M. Kuchlinga4c2b742003-04-25 00:26:51 +00001274 def __init__(self, uri, transport=None, encoding=None, verbose=0,
1275 allow_none=0):
Fredrik Lundhb9056332001-07-11 17:42:21 +00001276 # establish a "logical" server connection
1277
1278 # get the url
Fredrik Lundhc4c062f2001-09-10 19:45:02 +00001279 import urllib
Fredrik Lundhb9056332001-07-11 17:42:21 +00001280 type, uri = urllib.splittype(uri)
1281 if type not in ("http", "https"):
1282 raise IOError, "unsupported XML-RPC protocol"
1283 self.__host, self.__handler = urllib.splithost(uri)
1284 if not self.__handler:
1285 self.__handler = "/RPC2"
1286
1287 if transport is None:
1288 if type == "https":
1289 transport = SafeTransport()
1290 else:
1291 transport = Transport()
1292 self.__transport = transport
1293
1294 self.__encoding = encoding
1295 self.__verbose = verbose
Andrew M. Kuchlinga4c2b742003-04-25 00:26:51 +00001296 self.__allow_none = allow_none
Tim Petersc2659cf2003-05-12 20:19:37 +00001297
Fredrik Lundhb9056332001-07-11 17:42:21 +00001298 def __request(self, methodname, params):
1299 # call a method on the remote server
1300
Andrew M. Kuchlinga4c2b742003-04-25 00:26:51 +00001301 request = dumps(params, methodname, encoding=self.__encoding,
1302 allow_none=self.__allow_none)
Fredrik Lundhb9056332001-07-11 17:42:21 +00001303
1304 response = self.__transport.request(
1305 self.__host,
1306 self.__handler,
1307 request,
1308 verbose=self.__verbose
1309 )
1310
1311 if len(response) == 1:
1312 response = response[0]
1313
1314 return response
1315
1316 def __repr__(self):
1317 return (
Fredrik Lundh78eedce2001-08-23 20:04:33 +00001318 "<ServerProxy for %s%s>" %
Fredrik Lundhb9056332001-07-11 17:42:21 +00001319 (self.__host, self.__handler)
1320 )
1321
1322 __str__ = __repr__
1323
1324 def __getattr__(self, name):
1325 # magic method dispatcher
1326 return _Method(self.__request, name)
1327
1328 # note: to call a remote object with an non-standard name, use
1329 # result getattr(server, "strange-python-name")(args)
1330
Fredrik Lundh78eedce2001-08-23 20:04:33 +00001331# compatibility
Fredrik Lundh3d9addd2002-06-27 21:36:21 +00001332
Fredrik Lundhb9056332001-07-11 17:42:21 +00001333Server = ServerProxy
1334
1335# --------------------------------------------------------------------
1336# test code
1337
1338if __name__ == "__main__":
1339
1340 # simple test program (from the XML-RPC specification)
1341
Fredrik Lundh78eedce2001-08-23 20:04:33 +00001342 # server = ServerProxy("http://localhost:8000") # local server
Tim Petersc2659cf2003-05-12 20:19:37 +00001343 server = ServerProxy("http://betty.userland.com")
Fredrik Lundhb9056332001-07-11 17:42:21 +00001344
1345 print server
1346
1347 try:
1348 print server.examples.getStateName(41)
1349 except Error, v:
1350 print "ERROR", v