annotate Lib/xmlrpclib.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents c02e790c4535
children 8c2635afbfe1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1 #
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
2 # XML-RPC CLIENT LIBRARY
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
3 # $Id$
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
4 #
19835
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
5 # an XML-RPC client interface for Python.
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
6 #
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
7 # the marshalling and response parser code can also be used to
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
8 # implement XML-RPC servers.
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
9 #
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
10 # Notes:
33209
34a8d75edd2b Bump minimum Python version to 2.1. Fixes #1009803.
Martin v. Löwis <martin@v.loewis.de>
parents: 32249
diff changeset
11 # this version is designed to work with Python 2.1 or newer.
19835
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
12 #
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
13 # History:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
14 # 1999-01-14 fl Created
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
15 # 1999-01-15 fl Changed dateTime to use localtime
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
16 # 1999-01-16 fl Added Binary/base64 element, default to RPC2 service
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
17 # 1999-01-19 fl Fixed array data element (from Skip Montanaro)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
18 # 1999-01-21 fl Fixed dateTime constructor, etc.
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
19 # 1999-02-02 fl Added fault handling, handle empty sequences, etc.
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
20 # 1999-02-10 fl Fixed problem with empty responses (from Skip Montanaro)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
21 # 1999-06-20 fl Speed improvements, pluggable parsers/transports (0.9.8)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
22 # 2000-11-28 fl Changed boolean to check the truth value of its argument
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
23 # 2001-02-24 fl Added encoding/Unicode/SafeTransport patches
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
24 # 2001-02-26 fl Added compare support to wrappers (0.9.9/1.0b1)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
25 # 2001-03-28 fl Make sure response tuple is a singleton
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
26 # 2001-03-29 fl Don't require empty params element (from Nicholas Riley)
19450
cee1decc4ff2 updated to current PythonWare version (1.0b3). fixed type checks in
Fredrik Lundh <fredrik@pythonware.com>
parents: 18477
diff changeset
27 # 2001-06-10 fl Folded in _xmlrpclib accelerator support (1.0b2)
cee1decc4ff2 updated to current PythonWare version (1.0b3). fixed type checks in
Fredrik Lundh <fredrik@pythonware.com>
parents: 18477
diff changeset
28 # 2001-08-20 fl Base xmlrpclib.Error on built-in Exception (from Paul Prescod)
19835
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
29 # 2001-09-03 fl Allow Transport subclass to override getparser
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
30 # 2001-09-10 fl Lazy import of urllib, cgi, xmllib (20x import speedup)
20208
ba5aaa6bc1e5 restored 1.5.2 compatibility
Fredrik Lundh <fredrik@pythonware.com>
parents: 20206
diff changeset
31 # 2001-10-01 fl Remove containers from memo cache when done with them
ba5aaa6bc1e5 restored 1.5.2 compatibility
Fredrik Lundh <fredrik@pythonware.com>
parents: 20206
diff changeset
32 # 2001-10-01 fl Use faster escape method (80% dumps speedup)
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
33 # 2001-10-02 fl More dumps microtuning
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
34 # 2001-10-04 fl Make sure import expat gets a parser (from Guido van Rossum)
20363
6b1d8cb9904a allow long ints to be marshalled as ints - no check is made to the incoming
Skip Montanaro <skip@pobox.com>
parents: 20216
diff changeset
35 # 2001-10-10 sm Allow long ints to be passed as ints if they don't overflow
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
36 # 2001-10-17 sm Test for int and long overflow (allows use on 64-bit systems)
21494
44c479f84e36 partial merge with current pythonware codebase:
Fredrik Lundh <fredrik@pythonware.com>
parents: 20504
diff changeset
37 # 2001-11-12 fl Use repr() to marshal doubles (from Paul Felix)
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
38 # 2002-03-17 fl Avoid buffered read when possible (from James Rucker)
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
39 # 2002-04-07 fl Added pythondoc comments
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
40 # 2002-04-16 fl Added __str__ methods to datetime/binary wrappers
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
41 # 2002-05-15 fl Added error constants (from Andrew Kuchling)
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
42 # 2002-06-27 fl Merged with Python CVS version
25882
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
43 # 2002-10-22 fl Added basic authentication (based on code from Phillip Eby)
29825
a37106ac7c8c Patch #764470: Fix marshalling of faults. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 29489
diff changeset
44 # 2003-01-22 sm Add support for the bool type
a37106ac7c8c Patch #764470: Fix marshalling of faults. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 29489
diff changeset
45 # 2003-02-27 gvr Remove apply calls
a37106ac7c8c Patch #764470: Fix marshalling of faults. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 29489
diff changeset
46 # 2003-04-24 sm Use cStringIO if available
a37106ac7c8c Patch #764470: Fix marshalling of faults. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 29489
diff changeset
47 # 2003-04-25 ak Add support for nil
a37106ac7c8c Patch #764470: Fix marshalling of faults. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 29489
diff changeset
48 # 2003-06-15 gn Add support for time.struct_time
a37106ac7c8c Patch #764470: Fix marshalling of faults. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 29489
diff changeset
49 # 2003-07-12 gp Correct marshalling of Faults
30822
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
50 # 2003-10-31 mvl Add multicall support
33209
34a8d75edd2b Bump minimum Python version to 2.1. Fixes #1009803.
Martin v. Löwis <martin@v.loewis.de>
parents: 32249
diff changeset
51 # 2004-08-20 mvl Bump minimum supported Python version to 2.1
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
52 #
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
53 # Copyright (c) 1999-2002 by Secret Labs AB.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
54 # Copyright (c) 1999-2002 by Fredrik Lundh.
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
55 #
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
56 # info@pythonware.com
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
57 # http://www.pythonware.com
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
58 #
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
59 # --------------------------------------------------------------------
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
60 # The XML-RPC client interface is
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
61 #
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
62 # Copyright (c) 1999-2002 by Secret Labs AB
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
63 # Copyright (c) 1999-2002 by Fredrik Lundh
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
64 #
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
65 # By obtaining, using, and/or copying this software and/or its
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
66 # associated documentation, you agree that you have read, understood,
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
67 # and will comply with the following terms and conditions:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
68 #
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
69 # Permission to use, copy, modify, and distribute this software and
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
70 # its associated documentation for any purpose and without fee is
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
71 # hereby granted, provided that the above copyright notice appears in
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
72 # all copies, and that both that copyright notice and this permission
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
73 # notice appear in supporting documentation, and that the name of
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
74 # Secret Labs AB or the author not be used in advertising or publicity
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
75 # pertaining to distribution of the software without specific, written
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
76 # prior permission.
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
77 #
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
78 # SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
79 # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
80 # ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
81 # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
82 # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
83 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
84 # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
85 # OF THIS SOFTWARE.
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
86 # --------------------------------------------------------------------
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
87
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
88 #
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
89 # things to look into some day:
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
90
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
91 # TODO: sort out True/False/boolean issues for Python 2.3
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
92
19691
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
93 """
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
94 An XML-RPC client interface for Python.
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
95
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
96 The marshalling and response parser code can also be used to
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
97 implement XML-RPC servers.
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
98
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
99 Exported exceptions:
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
100
19835
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
101 Error Base class for client errors
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
102 ProtocolError Indicates an HTTP protocol error
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
103 ResponseError Indicates a broken response package
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
104 Fault Indicates an XML-RPC fault package
19691
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
105
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
106 Exported classes:
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
107
19835
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
108 ServerProxy Represents a logical connection to an XML-RPC server
19691
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
109
30838
19bb1b2fb233 SF patch #834015: Remove imports of unused modules
Raymond Hettinger <python@rcn.com>
parents: 30822
diff changeset
110 MultiCall Executor of boxcared xmlrpc requests
19835
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
111 Boolean boolean wrapper to generate a "boolean" XML-RPC value
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
112 DateTime dateTime wrapper for an ISO 8601 string or time tuple or
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
113 localtime integer value to generate a "dateTime.iso8601"
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
114 XML-RPC value
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
115 Binary binary data wrapper
19691
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
116
19835
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
117 SlowParser Slow but safe standard parser (based on xmllib)
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
118 Marshaller Generate an XML-RPC params chunk from a Python data structure
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
119 Unmarshaller Unmarshal an XML-RPC response from incoming XML event message
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
120 Transport Handles an HTTP transaction to an XML-RPC server
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
121 SafeTransport Handles an HTTPS transaction to an XML-RPC server
19691
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
122
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
123 Exported constants:
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
124
19835
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
125 True
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
126 False
19691
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
127
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
128 Exported functions:
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
129
19835
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
130 boolean Convert any Python value to an XML-RPC boolean
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
131 getparser Create instance of the fastest available parser & attach
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
132 to an unmarshalling object
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
133 dumps Convert an argument tuple or a Fault instance to an XML-RPC
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
134 request (or response, if the methodresponse option is used).
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
135 loads Convert an XML-RPC packet to unmarshalled data plus a method
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
136 name (None if not present).
19691
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
137 """
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
138
20481
cd96c4b01ff0 Remove unused import; reported by Neal Norwitz.
Fred Drake <fdrake@acm.org>
parents: 20363
diff changeset
139 import re, string, time, operator
20208
ba5aaa6bc1e5 restored 1.5.2 compatibility
Fredrik Lundh <fredrik@pythonware.com>
parents: 20206
diff changeset
140
34552
a24e70f4684b Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents: 34551
diff changeset
141 from types import *
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
142 import socket
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
143 import errno
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
144 import httplib
55343
3a613e00f66d http://bugs.python.org/issue6499
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 55089
diff changeset
145 try:
3a613e00f66d http://bugs.python.org/issue6499
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 55089
diff changeset
146 import gzip
3a613e00f66d http://bugs.python.org/issue6499
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 55089
diff changeset
147 except ImportError:
3a613e00f66d http://bugs.python.org/issue6499
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 55089
diff changeset
148 gzip = None #python can be built without zlib/gzip support
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
149
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
150 # --------------------------------------------------------------------
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
151 # Internal stuff
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
152
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
153 try:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
154 unicode
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
155 except NameError:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
156 unicode = None # unicode support not available
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
157
27345
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
158 try:
34578
607c48b5efd7 accept datetime.datetime instances when marshalling;
Fred Drake <fdrake@acm.org>
parents: 34552
diff changeset
159 import datetime
607c48b5efd7 accept datetime.datetime instances when marshalling;
Fred Drake <fdrake@acm.org>
parents: 34552
diff changeset
160 except ImportError:
607c48b5efd7 accept datetime.datetime instances when marshalling;
Fred Drake <fdrake@acm.org>
parents: 34552
diff changeset
161 datetime = None
607c48b5efd7 accept datetime.datetime instances when marshalling;
Fred Drake <fdrake@acm.org>
parents: 34552
diff changeset
162
607c48b5efd7 accept datetime.datetime instances when marshalling;
Fred Drake <fdrake@acm.org>
parents: 34552
diff changeset
163 try:
27345
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
164 _bool_is_builtin = False.__class__.__name__ == "bool"
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
165 except NameError:
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
166 _bool_is_builtin = 0
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
167
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
168 def _decode(data, encoding, is8bit=re.compile("[\x80-\xff]").search):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
169 # decode non-ascii string (if possible)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
170 if unicode and encoding and is8bit(data):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
171 data = unicode(data, encoding)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
172 return data
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
173
20208
ba5aaa6bc1e5 restored 1.5.2 compatibility
Fredrik Lundh <fredrik@pythonware.com>
parents: 20206
diff changeset
174 def escape(s, replace=string.replace):
ba5aaa6bc1e5 restored 1.5.2 compatibility
Fredrik Lundh <fredrik@pythonware.com>
parents: 20206
diff changeset
175 s = replace(s, "&", "&amp;")
ba5aaa6bc1e5 restored 1.5.2 compatibility
Fredrik Lundh <fredrik@pythonware.com>
parents: 20206
diff changeset
176 s = replace(s, "<", "&lt;")
ba5aaa6bc1e5 restored 1.5.2 compatibility
Fredrik Lundh <fredrik@pythonware.com>
parents: 20206
diff changeset
177 return replace(s, ">", "&gt;",)
ba5aaa6bc1e5 restored 1.5.2 compatibility
Fredrik Lundh <fredrik@pythonware.com>
parents: 20206
diff changeset
178
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
179 if unicode:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
180 def _stringify(string):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
181 # convert to 7-bit ascii if possible
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
182 try:
34582
0a6e779d347d fix decoding in _stringify to not depend on the default encoding
Fred Drake <fdrake@acm.org>
parents: 34578
diff changeset
183 return string.encode("ascii")
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
184 except UnicodeError:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
185 return string
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
186 else:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
187 def _stringify(string):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
188 return string
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
189
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
190 __version__ = "1.0.1"
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
191
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
192 # xmlrpc integer limits
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
193 MAXINT = 2L**31-1
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
194 MININT = -2L**31
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
195
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
196 # --------------------------------------------------------------------
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
197 # Error constants (from Dan Libby's specification at
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
198 # http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php)
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
199
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
200 # Ranges of errors
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
201 PARSE_ERROR = -32700
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
202 SERVER_ERROR = -32600
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
203 APPLICATION_ERROR = -32500
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
204 SYSTEM_ERROR = -32400
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
205 TRANSPORT_ERROR = -32300
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
206
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
207 # Specific errors
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
208 NOT_WELLFORMED_ERROR = -32700
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
209 UNSUPPORTED_ENCODING = -32701
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
210 INVALID_ENCODING_CHAR = -32702
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
211 INVALID_XMLRPC = -32600
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
212 METHOD_NOT_FOUND = -32601
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
213 INVALID_METHOD_PARAMS = -32602
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
214 INTERNAL_ERROR = -32603
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
215
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
216 # --------------------------------------------------------------------
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
217 # Exceptions
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
218
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
219 ##
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
220 # Base class for all kinds of client-side errors.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
221
19450
cee1decc4ff2 updated to current PythonWare version (1.0b3). fixed type checks in
Fredrik Lundh <fredrik@pythonware.com>
parents: 18477
diff changeset
222 class Error(Exception):
19691
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
223 """Base class for client errors."""
19840
377746a38972 more xmlrpclib tweaks: fixed repr(Fault()); enable UTF-8 parsing in
Fredrik Lundh <fredrik@pythonware.com>
parents: 19835
diff changeset
224 def __str__(self):
377746a38972 more xmlrpclib tweaks: fixed repr(Fault()); enable UTF-8 parsing in
Fredrik Lundh <fredrik@pythonware.com>
parents: 19835
diff changeset
225 return repr(self)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
226
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
227 ##
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
228 # Indicates an HTTP-level protocol error. This is raised by the HTTP
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
229 # transport layer, if the server returns an error code other than 200
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
230 # (OK).
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
231 #
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
232 # @param url The target URL.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
233 # @param errcode The HTTP error code.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
234 # @param errmsg The HTTP error message.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
235 # @param headers The HTTP header dictionary.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
236
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
237 class ProtocolError(Error):
19691
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
238 """Indicates an HTTP protocol error."""
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
239 def __init__(self, url, errcode, errmsg, headers):
19840
377746a38972 more xmlrpclib tweaks: fixed repr(Fault()); enable UTF-8 parsing in
Fredrik Lundh <fredrik@pythonware.com>
parents: 19835
diff changeset
240 Error.__init__(self)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
241 self.url = url
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
242 self.errcode = errcode
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
243 self.errmsg = errmsg
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
244 self.headers = headers
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
245 def __repr__(self):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
246 return (
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
247 "<ProtocolError for %s: %s %s>" %
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
248 (self.url, self.errcode, self.errmsg)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
249 )
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
250
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
251 ##
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
252 # Indicates a broken XML-RPC response package. This exception is
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
253 # raised by the unmarshalling layer, if the XML-RPC response is
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
254 # malformed.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
255
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
256 class ResponseError(Error):
19835
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
257 """Indicates a broken response package."""
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
258 pass
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
259
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
260 ##
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
261 # Indicates an XML-RPC fault response package. This exception is
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
262 # raised by the unmarshalling layer, if the XML-RPC response contains
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
263 # a fault string. This exception can also used as a class, to
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
264 # generate a fault XML-RPC message.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
265 #
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
266 # @param faultCode The XML-RPC fault code.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
267 # @param faultString The XML-RPC fault string.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
268
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
269 class Fault(Error):
19835
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
270 """Indicates an XML-RPC fault package."""
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
271 def __init__(self, faultCode, faultString, **extra):
19840
377746a38972 more xmlrpclib tweaks: fixed repr(Fault()); enable UTF-8 parsing in
Fredrik Lundh <fredrik@pythonware.com>
parents: 19835
diff changeset
272 Error.__init__(self)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
273 self.faultCode = faultCode
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
274 self.faultString = faultString
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
275 def __repr__(self):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
276 return (
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
277 "<Fault %s: %s>" %
19840
377746a38972 more xmlrpclib tweaks: fixed repr(Fault()); enable UTF-8 parsing in
Fredrik Lundh <fredrik@pythonware.com>
parents: 19835
diff changeset
278 (self.faultCode, repr(self.faultString))
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
279 )
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
280
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
281 # --------------------------------------------------------------------
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
282 # Special values
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
283
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
284 ##
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
285 # Wrapper for XML-RPC boolean values. Use the xmlrpclib.True and
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
286 # xmlrpclib.False constants, or the xmlrpclib.boolean() function, to
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
287 # generate boolean XML-RPC values.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
288 #
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
289 # @param value A boolean value. Any true value is interpreted as True,
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
290 # all other values are interpreted as False.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
291
48160
21f146387a63 Remove assignment to True/False and use of dict.has_key() to silence warnings
Brett Cannon <bcannon@gmail.com>
parents: 47369
diff changeset
292 from sys import modules
21f146387a63 Remove assignment to True/False and use of dict.has_key() to silence warnings
Brett Cannon <bcannon@gmail.com>
parents: 47369
diff changeset
293 mod_dict = modules[__name__].__dict__
27345
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
294 if _bool_is_builtin:
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
295 boolean = Boolean = bool
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
296 # to avoid breaking code which references xmlrpclib.{True,False}
48160
21f146387a63 Remove assignment to True/False and use of dict.has_key() to silence warnings
Brett Cannon <bcannon@gmail.com>
parents: 47369
diff changeset
297 mod_dict['True'] = True
21f146387a63 Remove assignment to True/False and use of dict.has_key() to silence warnings
Brett Cannon <bcannon@gmail.com>
parents: 47369
diff changeset
298 mod_dict['False'] = False
27345
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
299 else:
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
300 class Boolean:
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
301 """Boolean-value wrapper.
19691
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
302
27345
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
303 Use True or False to generate a "boolean" XML-RPC value.
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
304 """
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
305
27345
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
306 def __init__(self, value = 0):
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
307 self.value = operator.truth(value)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
308
27345
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
309 def encode(self, out):
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
310 out.write("<value><boolean>%d</boolean></value>\n" % self.value)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
311
27345
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
312 def __cmp__(self, other):
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
313 if isinstance(other, Boolean):
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
314 other = other.value
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
315 return cmp(self.value, other)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
316
27345
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
317 def __repr__(self):
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
318 if self.value:
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
319 return "<Boolean True at %x>" % id(self)
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
320 else:
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
321 return "<Boolean False at %x>" % id(self)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
322
27345
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
323 def __int__(self):
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
324 return self.value
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
325
27345
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
326 def __nonzero__(self):
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
327 return self.value
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
328
48160
21f146387a63 Remove assignment to True/False and use of dict.has_key() to silence warnings
Brett Cannon <bcannon@gmail.com>
parents: 47369
diff changeset
329 mod_dict['True'] = Boolean(1)
21f146387a63 Remove assignment to True/False and use of dict.has_key() to silence warnings
Brett Cannon <bcannon@gmail.com>
parents: 47369
diff changeset
330 mod_dict['False'] = Boolean(0)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
331
27345
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
332 ##
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
333 # Map true or false value to XML-RPC boolean values.
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
334 #
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
335 # @def boolean(value)
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
336 # @param value A boolean value. Any true value is mapped to True,
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
337 # all other values are mapped to False.
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
338 # @return xmlrpclib.True or xmlrpclib.False.
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
339 # @see Boolean
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
340 # @see True
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
341 # @see False
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
342
27345
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
343 def boolean(value, _truefalse=(False, True)):
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
344 """Convert any Python value to XML-RPC 'boolean'."""
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
345 return _truefalse[operator.truth(value)]
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
346
48160
21f146387a63 Remove assignment to True/False and use of dict.has_key() to silence warnings
Brett Cannon <bcannon@gmail.com>
parents: 47369
diff changeset
347 del modules, mod_dict
21f146387a63 Remove assignment to True/False and use of dict.has_key() to silence warnings
Brett Cannon <bcannon@gmail.com>
parents: 47369
diff changeset
348
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
349 ##
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
350 # Wrapper for XML-RPC DateTime values. This converts a time value to
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
351 # the format used by XML-RPC.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
352 # <p>
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
353 # The value can be given as a string in the format
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
354 # "yyyymmddThh:mm:ss", as a 9-item time tuple (as returned by
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
355 # time.localtime()), or an integer value (as returned by time.time()).
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
356 # The wrapper uses time.localtime() to convert an integer to a time
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
357 # tuple.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
358 #
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
359 # @param value The time, given as an ISO 8601 string, a time
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
360 # tuple, or a integer time value.
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
361
46471
4f8261b92fc5 resolve issue 2014
Skip Montanaro <skip@pobox.com>
parents: 45459
diff changeset
362 def _strftime(value):
4f8261b92fc5 resolve issue 2014
Skip Montanaro <skip@pobox.com>
parents: 45459
diff changeset
363 if datetime:
4f8261b92fc5 resolve issue 2014
Skip Montanaro <skip@pobox.com>
parents: 45459
diff changeset
364 if isinstance(value, datetime.datetime):
4f8261b92fc5 resolve issue 2014
Skip Montanaro <skip@pobox.com>
parents: 45459
diff changeset
365 return "%04d%02d%02dT%02d:%02d:%02d" % (
4f8261b92fc5 resolve issue 2014
Skip Montanaro <skip@pobox.com>
parents: 45459
diff changeset
366 value.year, value.month, value.day,
4f8261b92fc5 resolve issue 2014
Skip Montanaro <skip@pobox.com>
parents: 45459
diff changeset
367 value.hour, value.minute, value.second)
4f8261b92fc5 resolve issue 2014
Skip Montanaro <skip@pobox.com>
parents: 45459
diff changeset
368
4f8261b92fc5 resolve issue 2014
Skip Montanaro <skip@pobox.com>
parents: 45459
diff changeset
369 if not isinstance(value, (TupleType, time.struct_time)):
4f8261b92fc5 resolve issue 2014
Skip Montanaro <skip@pobox.com>
parents: 45459
diff changeset
370 if value == 0:
4f8261b92fc5 resolve issue 2014
Skip Montanaro <skip@pobox.com>
parents: 45459
diff changeset
371 value = time.time()
4f8261b92fc5 resolve issue 2014
Skip Montanaro <skip@pobox.com>
parents: 45459
diff changeset
372 value = time.localtime(value)
4f8261b92fc5 resolve issue 2014
Skip Montanaro <skip@pobox.com>
parents: 45459
diff changeset
373
4f8261b92fc5 resolve issue 2014
Skip Montanaro <skip@pobox.com>
parents: 45459
diff changeset
374 return "%04d%02d%02dT%02d:%02d:%02d" % value[:6]
4f8261b92fc5 resolve issue 2014
Skip Montanaro <skip@pobox.com>
parents: 45459
diff changeset
375
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
376 class DateTime:
19835
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
377 """DateTime wrapper for an ISO 8601 string or time tuple or
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
378 localtime integer value to generate 'dateTime.iso8601' XML-RPC
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
379 value.
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
380 """
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
381
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
382 def __init__(self, value=0):
46471
4f8261b92fc5 resolve issue 2014
Skip Montanaro <skip@pobox.com>
parents: 45459
diff changeset
383 if isinstance(value, StringType):
4f8261b92fc5 resolve issue 2014
Skip Montanaro <skip@pobox.com>
parents: 45459
diff changeset
384 self.value = value
4f8261b92fc5 resolve issue 2014
Skip Montanaro <skip@pobox.com>
parents: 45459
diff changeset
385 else:
4f8261b92fc5 resolve issue 2014
Skip Montanaro <skip@pobox.com>
parents: 45459
diff changeset
386 self.value = _strftime(value)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
387
45459
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
388 def make_comparable(self, other):
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
389 if isinstance(other, DateTime):
45459
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
390 s = self.value
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
391 o = other.value
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
392 elif datetime and isinstance(other, datetime.datetime):
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
393 s = self.value
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
394 o = other.strftime("%Y%m%dT%H:%M:%S")
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
395 elif isinstance(other, (str, unicode)):
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
396 s = self.value
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
397 o = other
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
398 elif hasattr(other, "timetuple"):
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
399 s = self.timetuple()
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
400 o = other.timetuple()
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
401 else:
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
402 otype = (hasattr(other, "__class__")
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
403 and other.__class__.__name__
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
404 or type(other))
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
405 raise TypeError("Can't compare %s and %s" %
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
406 (self.__class__.__name__, otype))
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
407 return s, o
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
408
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
409 def __lt__(self, other):
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
410 s, o = self.make_comparable(other)
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
411 return s < o
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
412
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
413 def __le__(self, other):
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
414 s, o = self.make_comparable(other)
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
415 return s <= o
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
416
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
417 def __gt__(self, other):
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
418 s, o = self.make_comparable(other)
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
419 return s > o
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
420
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
421 def __ge__(self, other):
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
422 s, o = self.make_comparable(other)
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
423 return s >= o
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
424
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
425 def __eq__(self, other):
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
426 s, o = self.make_comparable(other)
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
427 return s == o
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
428
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
429 def __ne__(self, other):
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
430 s, o = self.make_comparable(other)
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
431 return s != o
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
432
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
433 def timetuple(self):
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
434 return time.strptime(self.value, "%Y%m%dT%H:%M:%S")
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
435
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
436 def __cmp__(self, other):
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
437 s, o = self.make_comparable(other)
1cc47e129ff4 #1330538: Improve comparison of xmlrpclib.DateTime and datetime instances.
Andrew M. Kuchling <amk@amk.ca>
parents: 40360
diff changeset
438 return cmp(s, o)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
439
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
440 ##
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
441 # Get date/time value.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
442 #
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
443 # @return Date/time value, as an ISO 8601 string.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
444
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
445 def __str__(self):
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
446 return self.value
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
447
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
448 def __repr__(self):
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
449 return "<DateTime %s at %x>" % (repr(self.value), id(self))
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
450
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
451 def decode(self, data):
35984
75d536b247ea [Bug #1164912] Ensure Datetime wrapper class .value attribute is an 8-bit string, not a Unicode string
Andrew M. Kuchling <amk@amk.ca>
parents: 34956
diff changeset
452 data = str(data)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
453 self.value = string.strip(data)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
454
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
455 def encode(self, out):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
456 out.write("<value><dateTime.iso8601>")
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
457 out.write(self.value)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
458 out.write("</dateTime.iso8601></value>\n")
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
459
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
460 def _datetime(data):
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
461 # decode xml element contents into a DateTime structure.
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
462 value = DateTime()
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
463 value.decode(data)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
464 return value
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
465
34956
404088503a78 Add better datetime support to xmlrpclib module. Closes patch #1120353.
Skip Montanaro <skip@pobox.com>
parents: 34582
diff changeset
466 def _datetime_type(data):
404088503a78 Add better datetime support to xmlrpclib module. Closes patch #1120353.
Skip Montanaro <skip@pobox.com>
parents: 34582
diff changeset
467 t = time.strptime(data, "%Y%m%dT%H:%M:%S")
404088503a78 Add better datetime support to xmlrpclib module. Closes patch #1120353.
Skip Montanaro <skip@pobox.com>
parents: 34582
diff changeset
468 return datetime.datetime(*tuple(t)[:6])
404088503a78 Add better datetime support to xmlrpclib module. Closes patch #1120353.
Skip Montanaro <skip@pobox.com>
parents: 34582
diff changeset
469
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
470 ##
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
471 # Wrapper for binary data. This can be used to transport any kind
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
472 # of binary data over XML-RPC, using BASE64 encoding.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
473 #
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
474 # @param data An 8-bit string containing arbitrary data.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
475
28802
25c8cee4457e move imports in Binary class to top level to avoid repeated imports.
Skip Montanaro <skip@pobox.com>
parents: 28089
diff changeset
476 import base64
25c8cee4457e move imports in Binary class to top level to avoid repeated imports.
Skip Montanaro <skip@pobox.com>
parents: 28089
diff changeset
477 try:
25c8cee4457e move imports in Binary class to top level to avoid repeated imports.
Skip Montanaro <skip@pobox.com>
parents: 28089
diff changeset
478 import cStringIO as StringIO
25c8cee4457e move imports in Binary class to top level to avoid repeated imports.
Skip Montanaro <skip@pobox.com>
parents: 28089
diff changeset
479 except ImportError:
25c8cee4457e move imports in Binary class to top level to avoid repeated imports.
Skip Montanaro <skip@pobox.com>
parents: 28089
diff changeset
480 import StringIO
25c8cee4457e move imports in Binary class to top level to avoid repeated imports.
Skip Montanaro <skip@pobox.com>
parents: 28089
diff changeset
481
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
482 class Binary:
19691
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
483 """Wrapper for binary data."""
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
484
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
485 def __init__(self, data=None):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
486 self.data = data
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
487
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
488 ##
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
489 # Get buffer contents.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
490 #
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
491 # @return Buffer contents, as an 8-bit string.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
492
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
493 def __str__(self):
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
494 return self.data or ""
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
495
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
496 def __cmp__(self, other):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
497 if isinstance(other, Binary):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
498 other = other.data
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
499 return cmp(self.data, other)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
500
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
501 def decode(self, data):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
502 self.data = base64.decodestring(data)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
503
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
504 def encode(self, out):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
505 out.write("<value><base64>\n")
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
506 base64.encode(StringIO.StringIO(self.data), out)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
507 out.write("</base64></value>\n")
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
508
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
509 def _binary(data):
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
510 # decode xml element contents into a Binary structure
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
511 value = Binary()
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
512 value.decode(data)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
513 return value
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
514
27345
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
515 WRAPPERS = (DateTime, Binary)
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
516 if not _bool_is_builtin:
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
517 WRAPPERS = WRAPPERS + (Boolean,)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
518
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
519 # --------------------------------------------------------------------
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
520 # XML parsers
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
521
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
522 try:
33741
d1f0c7079b82 removed info@pythonware.com reference
Fredrik Lundh <fredrik@pythonware.com>
parents: 33223
diff changeset
523 # optional xmlrpclib accelerator
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
524 import _xmlrpclib
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
525 FastParser = _xmlrpclib.Parser
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
526 FastUnmarshaller = _xmlrpclib.Unmarshaller
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
527 except (AttributeError, ImportError):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
528 FastParser = FastUnmarshaller = None
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
529
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
530 try:
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
531 import _xmlrpclib
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
532 FastMarshaller = _xmlrpclib.Marshaller
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
533 except (AttributeError, ImportError):
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
534 FastMarshaller = None
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
535
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
536 try:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
537 from xml.parsers import expat
20216
a5a330646c13 Under certain conditions (sometimes triggered by the test suite),
Guido van Rossum <guido@python.org>
parents: 20208
diff changeset
538 if not hasattr(expat, "ParserCreate"):
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
539 raise ImportError
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
540 except ImportError:
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
541 ExpatParser = None # expat not available
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
542 else:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
543 class ExpatParser:
54561
97a0a8589014 #5767: remove sgmlop support from xmlrpclib; the sgmlop parser does not do much validation and is no longer much faster than e.g. the cElementTree XMLParser.
Georg Brandl <georg@python.org>
parents: 50785
diff changeset
544 # fast expat parser for Python 2.0 and later.
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
545 def __init__(self, target):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
546 self._parser = parser = expat.ParserCreate(None, None)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
547 self._target = target
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
548 parser.StartElementHandler = target.start
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
549 parser.EndElementHandler = target.end
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
550 parser.CharacterDataHandler = target.data
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
551 encoding = None
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
552 if not parser.returns_unicode:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
553 encoding = "utf-8"
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
554 target.xml(encoding, None)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
555
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
556 def feed(self, data):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
557 self._parser.Parse(data, 0)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
558
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
559 def close(self):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
560 self._parser.Parse("", 1) # end of data
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
561 del self._target, self._parser # get rid of circular references
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
562
19835
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
563 class SlowParser:
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
564 """Default XML parser (based on xmllib.XMLParser)."""
54561
97a0a8589014 #5767: remove sgmlop support from xmlrpclib; the sgmlop parser does not do much validation and is no longer much faster than e.g. the cElementTree XMLParser.
Georg Brandl <georg@python.org>
parents: 50785
diff changeset
565 # this is the slowest parser.
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
566 def __init__(self, target):
19835
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
567 import xmllib # lazy subclassing (!)
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
568 if xmllib.XMLParser not in SlowParser.__bases__:
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
569 SlowParser.__bases__ = (xmllib.XMLParser,)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
570 self.handle_xml = target.xml
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
571 self.unknown_starttag = target.start
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
572 self.handle_data = target.data
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
573 self.handle_cdata = target.data
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
574 self.unknown_endtag = target.end
19840
377746a38972 more xmlrpclib tweaks: fixed repr(Fault()); enable UTF-8 parsing in
Fredrik Lundh <fredrik@pythonware.com>
parents: 19835
diff changeset
575 try:
377746a38972 more xmlrpclib tweaks: fixed repr(Fault()); enable UTF-8 parsing in
Fredrik Lundh <fredrik@pythonware.com>
parents: 19835
diff changeset
576 xmllib.XMLParser.__init__(self, accept_utf8=1)
377746a38972 more xmlrpclib tweaks: fixed repr(Fault()); enable UTF-8 parsing in
Fredrik Lundh <fredrik@pythonware.com>
parents: 19835
diff changeset
577 except TypeError:
377746a38972 more xmlrpclib tweaks: fixed repr(Fault()); enable UTF-8 parsing in
Fredrik Lundh <fredrik@pythonware.com>
parents: 19835
diff changeset
578 xmllib.XMLParser.__init__(self) # pre-2.0
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
579
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
580 # --------------------------------------------------------------------
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
581 # XML-RPC marshalling and unmarshalling code
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
582
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
583 ##
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
584 # XML-RPC marshaller.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
585 #
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
586 # @param encoding Default encoding for 8-bit strings. The default
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
587 # value is None (interpreted as UTF-8).
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
588 # @see dumps
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
589
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
590 class Marshaller:
19691
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
591 """Generate an XML-RPC params chunk from a Python data structure.
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
592
19835
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
593 Create a Marshaller instance for each set of parameters, and use
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
594 the "dumps" method to convert your data (represented as a tuple)
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
595 to an XML-RPC params chunk. To write a fault response, pass a
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
596 Fault instance instead. You may prefer to use the "dumps" module
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
597 function for this purpose.
19691
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
598 """
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
599
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
600 # by the way, if you don't understand what's going on in here,
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
601 # that's perfectly ok.
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
602
28808
2ba20fc9f495 [Patch #628208] Add optional support for the 'nil' extension
Andrew M. Kuchling <amk@amk.ca>
parents: 28802
diff changeset
603 def __init__(self, encoding=None, allow_none=0):
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
604 self.memo = {}
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
605 self.data = None
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
606 self.encoding = encoding
28808
2ba20fc9f495 [Patch #628208] Add optional support for the 'nil' extension
Andrew M. Kuchling <amk@amk.ca>
parents: 28802
diff changeset
607 self.allow_none = allow_none
29087
526f37fb380d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 28808
diff changeset
608
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
609 dispatch = {}
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
610
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
611 def dumps(self, values):
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
612 out = []
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
613 write = out.append
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
614 dump = self.__dump
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
615 if isinstance(values, Fault):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
616 # fault instance
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
617 write("<fault>\n")
29825
a37106ac7c8c Patch #764470: Fix marshalling of faults. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 29489
diff changeset
618 dump({'faultCode': values.faultCode,
a37106ac7c8c Patch #764470: Fix marshalling of faults. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 29489
diff changeset
619 'faultString': values.faultString},
a37106ac7c8c Patch #764470: Fix marshalling of faults. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 29489
diff changeset
620 write)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
621 write("</fault>\n")
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
622 else:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
623 # parameter block
19451
2817f1d3adbe untabification
Fredrik Lundh <fredrik@pythonware.com>
parents: 19450
diff changeset
624 # FIXME: the xml-rpc specification allows us to leave out
2817f1d3adbe untabification
Fredrik Lundh <fredrik@pythonware.com>
parents: 19450
diff changeset
625 # the entire <params> block if there are no parameters.
2817f1d3adbe untabification
Fredrik Lundh <fredrik@pythonware.com>
parents: 19450
diff changeset
626 # however, changing this may break older code (including
2817f1d3adbe untabification
Fredrik Lundh <fredrik@pythonware.com>
parents: 19450
diff changeset
627 # old versions of xmlrpclib.py), so this is better left as
2817f1d3adbe untabification
Fredrik Lundh <fredrik@pythonware.com>
parents: 19450
diff changeset
628 # is for now. See @XMLRPC3 for more information. /F
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
629 write("<params>\n")
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
630 for v in values:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
631 write("<param>\n")
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
632 dump(v, write)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
633 write("</param>\n")
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
634 write("</params>\n")
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
635 result = string.join(out, "")
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
636 return result
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
637
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
638 def __dump(self, value, write):
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
639 try:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
640 f = self.dispatch[type(value)]
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
641 except KeyError:
40360
40166e5011f8 Patch #1070046: Marshal new-style objects like InstanceType
Martin v. Löwis <martin@v.loewis.de>
parents: 35984
diff changeset
642 # check if this object can be marshalled as a structure
40166e5011f8 Patch #1070046: Marshal new-style objects like InstanceType
Martin v. Löwis <martin@v.loewis.de>
parents: 35984
diff changeset
643 try:
40166e5011f8 Patch #1070046: Marshal new-style objects like InstanceType
Martin v. Löwis <martin@v.loewis.de>
parents: 35984
diff changeset
644 value.__dict__
40166e5011f8 Patch #1070046: Marshal new-style objects like InstanceType
Martin v. Löwis <martin@v.loewis.de>
parents: 35984
diff changeset
645 except:
40166e5011f8 Patch #1070046: Marshal new-style objects like InstanceType
Martin v. Löwis <martin@v.loewis.de>
parents: 35984
diff changeset
646 raise TypeError, "cannot marshal %s objects" % type(value)
40166e5011f8 Patch #1070046: Marshal new-style objects like InstanceType
Martin v. Löwis <martin@v.loewis.de>
parents: 35984
diff changeset
647 # check if this class is a sub-class of a basic type,
40166e5011f8 Patch #1070046: Marshal new-style objects like InstanceType
Martin v. Löwis <martin@v.loewis.de>
parents: 35984
diff changeset
648 # because we don't know how to marshal these types
40166e5011f8 Patch #1070046: Marshal new-style objects like InstanceType
Martin v. Löwis <martin@v.loewis.de>
parents: 35984
diff changeset
649 # (e.g. a string sub-class)
40166e5011f8 Patch #1070046: Marshal new-style objects like InstanceType
Martin v. Löwis <martin@v.loewis.de>
parents: 35984
diff changeset
650 for type_ in type(value).__mro__:
40166e5011f8 Patch #1070046: Marshal new-style objects like InstanceType
Martin v. Löwis <martin@v.loewis.de>
parents: 35984
diff changeset
651 if type_ in self.dispatch.keys():
40166e5011f8 Patch #1070046: Marshal new-style objects like InstanceType
Martin v. Löwis <martin@v.loewis.de>
parents: 35984
diff changeset
652 raise TypeError, "cannot marshal %s objects" % type(value)
40166e5011f8 Patch #1070046: Marshal new-style objects like InstanceType
Martin v. Löwis <martin@v.loewis.de>
parents: 35984
diff changeset
653 f = self.dispatch[InstanceType]
40166e5011f8 Patch #1070046: Marshal new-style objects like InstanceType
Martin v. Löwis <martin@v.loewis.de>
parents: 35984
diff changeset
654 f(self, value, write)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
655
28808
2ba20fc9f495 [Patch #628208] Add optional support for the 'nil' extension
Andrew M. Kuchling <amk@amk.ca>
parents: 28802
diff changeset
656 def dump_nil (self, value, write):
2ba20fc9f495 [Patch #628208] Add optional support for the 'nil' extension
Andrew M. Kuchling <amk@amk.ca>
parents: 28802
diff changeset
657 if not self.allow_none:
2ba20fc9f495 [Patch #628208] Add optional support for the 'nil' extension
Andrew M. Kuchling <amk@amk.ca>
parents: 28802
diff changeset
658 raise TypeError, "cannot marshal None unless allow_none is enabled"
2ba20fc9f495 [Patch #628208] Add optional support for the 'nil' extension
Andrew M. Kuchling <amk@amk.ca>
parents: 28802
diff changeset
659 write("<value><nil/></value>")
34552
a24e70f4684b Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents: 34551
diff changeset
660 dispatch[NoneType] = dump_nil
29087
526f37fb380d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 28808
diff changeset
661
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
662 def dump_int(self, value, write):
20504
e0864aac9b6b test for int and long int overflow (allows operation on 64-bit platforms)
Skip Montanaro <skip@pobox.com>
parents: 20481
diff changeset
663 # in case ints are > 32 bits
e0864aac9b6b test for int and long int overflow (allows operation on 64-bit platforms)
Skip Montanaro <skip@pobox.com>
parents: 20481
diff changeset
664 if value > MAXINT or value < MININT:
e0864aac9b6b test for int and long int overflow (allows operation on 64-bit platforms)
Skip Montanaro <skip@pobox.com>
parents: 20481
diff changeset
665 raise OverflowError, "int exceeds XML-RPC limits"
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
666 write("<value><int>")
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
667 write(str(value))
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
668 write("</int></value>\n")
34552
a24e70f4684b Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents: 34551
diff changeset
669 dispatch[IntType] = dump_int
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
670
27345
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
671 if _bool_is_builtin:
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
672 def dump_bool(self, value, write):
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
673 write("<value><boolean>")
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
674 write(value and "1" or "0")
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
675 write("</boolean></value>\n")
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
676 dispatch[bool] = dump_bool
180067c2880f add support for Python's bool type to xmlrpclib - patch # 559288
Skip Montanaro <skip@pobox.com>
parents: 25981
diff changeset
677
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
678 def dump_long(self, value, write):
20504
e0864aac9b6b test for int and long int overflow (allows operation on 64-bit platforms)
Skip Montanaro <skip@pobox.com>
parents: 20481
diff changeset
679 if value > MAXINT or value < MININT:
e0864aac9b6b test for int and long int overflow (allows operation on 64-bit platforms)
Skip Montanaro <skip@pobox.com>
parents: 20481
diff changeset
680 raise OverflowError, "long int exceeds XML-RPC limits"
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
681 write("<value><int>")
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
682 write(str(int(value)))
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
683 write("</int></value>\n")
34552
a24e70f4684b Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents: 34551
diff changeset
684 dispatch[LongType] = dump_long
20363
6b1d8cb9904a allow long ints to be marshalled as ints - no check is made to the incoming
Skip Montanaro <skip@pobox.com>
parents: 20216
diff changeset
685
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
686 def dump_double(self, value, write):
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
687 write("<value><double>")
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
688 write(repr(value))
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
689 write("</double></value>\n")
34552
a24e70f4684b Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents: 34551
diff changeset
690 dispatch[FloatType] = dump_double
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
691
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
692 def dump_string(self, value, write, escape=escape):
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
693 write("<value><string>")
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
694 write(escape(value))
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
695 write("</string></value>\n")
34552
a24e70f4684b Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents: 34551
diff changeset
696 dispatch[StringType] = dump_string
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
697
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
698 if unicode:
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
699 def dump_unicode(self, value, write, escape=escape):
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
700 value = value.encode(self.encoding)
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
701 write("<value><string>")
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
702 write(escape(value))
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
703 write("</string></value>\n")
34552
a24e70f4684b Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents: 34551
diff changeset
704 dispatch[UnicodeType] = dump_unicode
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
705
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
706 def dump_array(self, value, write):
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
707 i = id(value)
48160
21f146387a63 Remove assignment to True/False and use of dict.has_key() to silence warnings
Brett Cannon <bcannon@gmail.com>
parents: 47369
diff changeset
708 if i in self.memo:
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
709 raise TypeError, "cannot marshal recursive sequences"
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
710 self.memo[i] = None
20208
ba5aaa6bc1e5 restored 1.5.2 compatibility
Fredrik Lundh <fredrik@pythonware.com>
parents: 20206
diff changeset
711 dump = self.__dump
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
712 write("<value><array><data>\n")
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
713 for v in value:
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
714 dump(v, write)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
715 write("</data></array></value>\n")
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
716 del self.memo[i]
34552
a24e70f4684b Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents: 34551
diff changeset
717 dispatch[TupleType] = dump_array
a24e70f4684b Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents: 34551
diff changeset
718 dispatch[ListType] = dump_array
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
719
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
720 def dump_struct(self, value, write, escape=escape):
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
721 i = id(value)
48160
21f146387a63 Remove assignment to True/False and use of dict.has_key() to silence warnings
Brett Cannon <bcannon@gmail.com>
parents: 47369
diff changeset
722 if i in self.memo:
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
723 raise TypeError, "cannot marshal recursive dictionaries"
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
724 self.memo[i] = None
20208
ba5aaa6bc1e5 restored 1.5.2 compatibility
Fredrik Lundh <fredrik@pythonware.com>
parents: 20206
diff changeset
725 dump = self.__dump
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
726 write("<value><struct>\n")
32249
7409c0c370e2 [Bug #841757] Patch from /F to allow Unicode strings as struct keys
Andrew M. Kuchling <amk@amk.ca>
parents: 30838
diff changeset
727 for k, v in value.items():
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
728 write("<member>\n")
34552
a24e70f4684b Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents: 34551
diff changeset
729 if type(k) is not StringType:
a24e70f4684b Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents: 34551
diff changeset
730 if unicode and type(k) is UnicodeType:
32249
7409c0c370e2 [Bug #841757] Patch from /F to allow Unicode strings as struct keys
Andrew M. Kuchling <amk@amk.ca>
parents: 30838
diff changeset
731 k = k.encode(self.encoding)
7409c0c370e2 [Bug #841757] Patch from /F to allow Unicode strings as struct keys
Andrew M. Kuchling <amk@amk.ca>
parents: 30838
diff changeset
732 else:
7409c0c370e2 [Bug #841757] Patch from /F to allow Unicode strings as struct keys
Andrew M. Kuchling <amk@amk.ca>
parents: 30838
diff changeset
733 raise TypeError, "dictionary key must be string"
20208
ba5aaa6bc1e5 restored 1.5.2 compatibility
Fredrik Lundh <fredrik@pythonware.com>
parents: 20206
diff changeset
734 write("<name>%s</name>\n" % escape(k))
32249
7409c0c370e2 [Bug #841757] Patch from /F to allow Unicode strings as struct keys
Andrew M. Kuchling <amk@amk.ca>
parents: 30838
diff changeset
735 dump(v, write)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
736 write("</member>\n")
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
737 write("</struct></value>\n")
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
738 del self.memo[i]
34552
a24e70f4684b Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents: 34551
diff changeset
739 dispatch[DictType] = dump_struct
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
740
34578
607c48b5efd7 accept datetime.datetime instances when marshalling;
Fred Drake <fdrake@acm.org>
parents: 34552
diff changeset
741 if datetime:
607c48b5efd7 accept datetime.datetime instances when marshalling;
Fred Drake <fdrake@acm.org>
parents: 34552
diff changeset
742 def dump_datetime(self, value, write):
607c48b5efd7 accept datetime.datetime instances when marshalling;
Fred Drake <fdrake@acm.org>
parents: 34552
diff changeset
743 write("<value><dateTime.iso8601>")
46471
4f8261b92fc5 resolve issue 2014
Skip Montanaro <skip@pobox.com>
parents: 45459
diff changeset
744 write(_strftime(value))
34578
607c48b5efd7 accept datetime.datetime instances when marshalling;
Fred Drake <fdrake@acm.org>
parents: 34552
diff changeset
745 write("</dateTime.iso8601></value>\n")
607c48b5efd7 accept datetime.datetime instances when marshalling;
Fred Drake <fdrake@acm.org>
parents: 34552
diff changeset
746 dispatch[datetime.datetime] = dump_datetime
607c48b5efd7 accept datetime.datetime instances when marshalling;
Fred Drake <fdrake@acm.org>
parents: 34552
diff changeset
747
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
748 def dump_instance(self, value, write):
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
749 # check for special wrappers
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
750 if value.__class__ in WRAPPERS:
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
751 self.write = write
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
752 value.encode(self)
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
753 del self.write
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
754 else:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
755 # store instance attributes as a struct (really?)
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
756 self.dump_struct(value.__dict__, write)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
757 dispatch[InstanceType] = dump_instance
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
758
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
759 ##
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
760 # XML-RPC unmarshaller.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
761 #
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
762 # @see loads
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
763
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
764 class Unmarshaller:
19691
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
765 """Unmarshal an XML-RPC response, based on incoming XML event
19840
377746a38972 more xmlrpclib tweaks: fixed repr(Fault()); enable UTF-8 parsing in
Fredrik Lundh <fredrik@pythonware.com>
parents: 19835
diff changeset
766 messages (start, data, end). Call close() to get the resulting
19691
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
767 data structure.
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
768
19840
377746a38972 more xmlrpclib tweaks: fixed repr(Fault()); enable UTF-8 parsing in
Fredrik Lundh <fredrik@pythonware.com>
parents: 19835
diff changeset
769 Note that this reader is fairly tolerant, and gladly accepts bogus
377746a38972 more xmlrpclib tweaks: fixed repr(Fault()); enable UTF-8 parsing in
Fredrik Lundh <fredrik@pythonware.com>
parents: 19835
diff changeset
770 XML-RPC data without complaining (but not bogus XML).
19691
933e812974c3 Added docstring by Neal Norwitz. This closes SF bug #450981.
Fred Drake <fdrake@acm.org>
parents: 19451
diff changeset
771 """
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
772
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
773 # and again, if you don't understand what's going on in here,
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
774 # that's perfectly ok.
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
775
34956
404088503a78 Add better datetime support to xmlrpclib module. Closes patch #1120353.
Skip Montanaro <skip@pobox.com>
parents: 34582
diff changeset
776 def __init__(self, use_datetime=0):
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
777 self._type = None
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
778 self._stack = []
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
779 self._marks = []
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
780 self._data = []
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
781 self._methodname = None
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
782 self._encoding = "utf-8"
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
783 self.append = self._stack.append
34956
404088503a78 Add better datetime support to xmlrpclib module. Closes patch #1120353.
Skip Montanaro <skip@pobox.com>
parents: 34582
diff changeset
784 self._use_datetime = use_datetime
404088503a78 Add better datetime support to xmlrpclib module. Closes patch #1120353.
Skip Montanaro <skip@pobox.com>
parents: 34582
diff changeset
785 if use_datetime and not datetime:
404088503a78 Add better datetime support to xmlrpclib module. Closes patch #1120353.
Skip Montanaro <skip@pobox.com>
parents: 34582
diff changeset
786 raise ValueError, "the datetime module is not available"
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
787
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
788 def close(self):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
789 # return response tuple and target method
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
790 if self._type is None or self._marks:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
791 raise ResponseError()
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
792 if self._type == "fault":
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 27345
diff changeset
793 raise Fault(**self._stack[0])
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
794 return tuple(self._stack)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
795
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
796 def getmethodname(self):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
797 return self._methodname
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
798
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
799 #
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
800 # event handlers
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
801
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
802 def xml(self, encoding, standalone):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
803 self._encoding = encoding
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
804 # FIXME: assert standalone == 1 ???
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
805
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
806 def start(self, tag, attrs):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
807 # prepare to handle this element
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
808 if tag == "array" or tag == "struct":
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
809 self._marks.append(len(self._stack))
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
810 self._data = []
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
811 self._value = (tag == "value")
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
812
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
813 def data(self, text):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
814 self._data.append(text)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
815
20208
ba5aaa6bc1e5 restored 1.5.2 compatibility
Fredrik Lundh <fredrik@pythonware.com>
parents: 20206
diff changeset
816 def end(self, tag, join=string.join):
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
817 # call the appropriate end tag handler
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
818 try:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
819 f = self.dispatch[tag]
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
820 except KeyError:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
821 pass # unknown tag ?
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
822 else:
20208
ba5aaa6bc1e5 restored 1.5.2 compatibility
Fredrik Lundh <fredrik@pythonware.com>
parents: 20206
diff changeset
823 return f(self, join(self._data, ""))
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
824
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
825 #
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
826 # accelerator support
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
827
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
828 def end_dispatch(self, tag, data):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
829 # dispatch data
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
830 try:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
831 f = self.dispatch[tag]
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
832 except KeyError:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
833 pass # unknown tag ?
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
834 else:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
835 return f(self, data)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
836
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
837 #
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
838 # element decoders
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
839
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
840 dispatch = {}
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
841
28808
2ba20fc9f495 [Patch #628208] Add optional support for the 'nil' extension
Andrew M. Kuchling <amk@amk.ca>
parents: 28802
diff changeset
842 def end_nil (self, data):
2ba20fc9f495 [Patch #628208] Add optional support for the 'nil' extension
Andrew M. Kuchling <amk@amk.ca>
parents: 28802
diff changeset
843 self.append(None)
2ba20fc9f495 [Patch #628208] Add optional support for the 'nil' extension
Andrew M. Kuchling <amk@amk.ca>
parents: 28802
diff changeset
844 self._value = 0
2ba20fc9f495 [Patch #628208] Add optional support for the 'nil' extension
Andrew M. Kuchling <amk@amk.ca>
parents: 28802
diff changeset
845 dispatch["nil"] = end_nil
29087
526f37fb380d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 28808
diff changeset
846
20208
ba5aaa6bc1e5 restored 1.5.2 compatibility
Fredrik Lundh <fredrik@pythonware.com>
parents: 20206
diff changeset
847 def end_boolean(self, data):
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
848 if data == "0":
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
849 self.append(False)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
850 elif data == "1":
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
851 self.append(True)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
852 else:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
853 raise TypeError, "bad boolean value"
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
854 self._value = 0
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
855 dispatch["boolean"] = end_boolean
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
856
20208
ba5aaa6bc1e5 restored 1.5.2 compatibility
Fredrik Lundh <fredrik@pythonware.com>
parents: 20206
diff changeset
857 def end_int(self, data):
ba5aaa6bc1e5 restored 1.5.2 compatibility
Fredrik Lundh <fredrik@pythonware.com>
parents: 20206
diff changeset
858 self.append(int(data))
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
859 self._value = 0
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
860 dispatch["i4"] = end_int
47369
ef51fcda348b #2985: allow i8 in XMLRPC responses.
Georg Brandl <georg@python.org>
parents: 46471
diff changeset
861 dispatch["i8"] = end_int
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
862 dispatch["int"] = end_int
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
863
20208
ba5aaa6bc1e5 restored 1.5.2 compatibility
Fredrik Lundh <fredrik@pythonware.com>
parents: 20206
diff changeset
864 def end_double(self, data):
ba5aaa6bc1e5 restored 1.5.2 compatibility
Fredrik Lundh <fredrik@pythonware.com>
parents: 20206
diff changeset
865 self.append(float(data))
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
866 self._value = 0
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
867 dispatch["double"] = end_double
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
868
20208
ba5aaa6bc1e5 restored 1.5.2 compatibility
Fredrik Lundh <fredrik@pythonware.com>
parents: 20206
diff changeset
869 def end_string(self, data):
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
870 if self._encoding:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
871 data = _decode(data, self._encoding)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
872 self.append(_stringify(data))
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
873 self._value = 0
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
874 dispatch["string"] = end_string
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
875 dispatch["name"] = end_string # struct keys are always strings
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
876
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
877 def end_array(self, data):
24058
37ffce2d9c3c Code modernization. Replace v=s[i]; del s[i] with single lookup v=s.pop(i)
Raymond Hettinger <python@rcn.com>
parents: 24031
diff changeset
878 mark = self._marks.pop()
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
879 # map arrays to Python lists
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
880 self._stack[mark:] = [self._stack[mark:]]
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
881 self._value = 0
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
882 dispatch["array"] = end_array
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
883
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
884 def end_struct(self, data):
24058
37ffce2d9c3c Code modernization. Replace v=s[i]; del s[i] with single lookup v=s.pop(i)
Raymond Hettinger <python@rcn.com>
parents: 24031
diff changeset
885 mark = self._marks.pop()
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
886 # map structs to Python dictionaries
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
887 dict = {}
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
888 items = self._stack[mark:]
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
889 for i in range(0, len(items), 2):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
890 dict[_stringify(items[i])] = items[i+1]
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
891 self._stack[mark:] = [dict]
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
892 self._value = 0
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
893 dispatch["struct"] = end_struct
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
894
20208
ba5aaa6bc1e5 restored 1.5.2 compatibility
Fredrik Lundh <fredrik@pythonware.com>
parents: 20206
diff changeset
895 def end_base64(self, data):
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
896 value = Binary()
20208
ba5aaa6bc1e5 restored 1.5.2 compatibility
Fredrik Lundh <fredrik@pythonware.com>
parents: 20206
diff changeset
897 value.decode(data)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
898 self.append(value)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
899 self._value = 0
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
900 dispatch["base64"] = end_base64
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
901
20208
ba5aaa6bc1e5 restored 1.5.2 compatibility
Fredrik Lundh <fredrik@pythonware.com>
parents: 20206
diff changeset
902 def end_dateTime(self, data):
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
903 value = DateTime()
20208
ba5aaa6bc1e5 restored 1.5.2 compatibility
Fredrik Lundh <fredrik@pythonware.com>
parents: 20206
diff changeset
904 value.decode(data)
34956
404088503a78 Add better datetime support to xmlrpclib module. Closes patch #1120353.
Skip Montanaro <skip@pobox.com>
parents: 34582
diff changeset
905 if self._use_datetime:
404088503a78 Add better datetime support to xmlrpclib module. Closes patch #1120353.
Skip Montanaro <skip@pobox.com>
parents: 34582
diff changeset
906 value = _datetime_type(data)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
907 self.append(value)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
908 dispatch["dateTime.iso8601"] = end_dateTime
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
909
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
910 def end_value(self, data):
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
911 # if we stumble upon a value element with no internal
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
912 # elements, treat it as a string element
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
913 if self._value:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
914 self.end_string(data)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
915 dispatch["value"] = end_value
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
916
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
917 def end_params(self, data):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
918 self._type = "params"
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
919 dispatch["params"] = end_params
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
920
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
921 def end_fault(self, data):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
922 self._type = "fault"
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
923 dispatch["fault"] = end_fault
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
924
20208
ba5aaa6bc1e5 restored 1.5.2 compatibility
Fredrik Lundh <fredrik@pythonware.com>
parents: 20206
diff changeset
925 def end_methodName(self, data):
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
926 if self._encoding:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
927 data = _decode(data, self._encoding)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
928 self._methodname = data
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
929 self._type = "methodName" # no params
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
930 dispatch["methodName"] = end_methodName
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
931
30822
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
932 ## Multicall support
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
933 #
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
934
30822
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
935 class _MultiCallMethod:
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
936 # some lesser magic to store calls made to a MultiCall object
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
937 # for batch execution
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
938 def __init__(self, call_list, name):
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
939 self.__call_list = call_list
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
940 self.__name = name
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
941 def __getattr__(self, name):
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
942 return _MultiCallMethod(self.__call_list, "%s.%s" % (self.__name, name))
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
943 def __call__(self, *args):
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
944 self.__call_list.append((self.__name, args))
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
945
33223
8abd322a013a Replace yield with sequence class. Fixes #1009803.
Martin v. Löwis <martin@v.loewis.de>
parents: 33209
diff changeset
946 class MultiCallIterator:
30822
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
947 """Iterates over the results of a multicall. Exceptions are
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
948 thrown in response to xmlrpc faults."""
30838
19bb1b2fb233 SF patch #834015: Remove imports of unused modules
Raymond Hettinger <python@rcn.com>
parents: 30822
diff changeset
949
33223
8abd322a013a Replace yield with sequence class. Fixes #1009803.
Martin v. Löwis <martin@v.loewis.de>
parents: 33209
diff changeset
950 def __init__(self, results):
8abd322a013a Replace yield with sequence class. Fixes #1009803.
Martin v. Löwis <martin@v.loewis.de>
parents: 33209
diff changeset
951 self.results = results
8abd322a013a Replace yield with sequence class. Fixes #1009803.
Martin v. Löwis <martin@v.loewis.de>
parents: 33209
diff changeset
952
8abd322a013a Replace yield with sequence class. Fixes #1009803.
Martin v. Löwis <martin@v.loewis.de>
parents: 33209
diff changeset
953 def __getitem__(self, i):
8abd322a013a Replace yield with sequence class. Fixes #1009803.
Martin v. Löwis <martin@v.loewis.de>
parents: 33209
diff changeset
954 item = self.results[i]
8abd322a013a Replace yield with sequence class. Fixes #1009803.
Martin v. Löwis <martin@v.loewis.de>
parents: 33209
diff changeset
955 if type(item) == type({}):
8abd322a013a Replace yield with sequence class. Fixes #1009803.
Martin v. Löwis <martin@v.loewis.de>
parents: 33209
diff changeset
956 raise Fault(item['faultCode'], item['faultString'])
8abd322a013a Replace yield with sequence class. Fixes #1009803.
Martin v. Löwis <martin@v.loewis.de>
parents: 33209
diff changeset
957 elif type(item) == type([]):
8abd322a013a Replace yield with sequence class. Fixes #1009803.
Martin v. Löwis <martin@v.loewis.de>
parents: 33209
diff changeset
958 return item[0]
30822
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
959 else:
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
960 raise ValueError,\
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
961 "unexpected type in multicall result"
30838
19bb1b2fb233 SF patch #834015: Remove imports of unused modules
Raymond Hettinger <python@rcn.com>
parents: 30822
diff changeset
962
30822
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
963 class MultiCall:
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
964 """server -> a object used to boxcar method calls
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
965
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
966 server should be a ServerProxy object.
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
967
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
968 Methods can be added to the MultiCall using normal
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
969 method call syntax e.g.:
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
970
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
971 multicall = MultiCall(server_proxy)
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
972 multicall.add(2,3)
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
973 multicall.get_address("Guido")
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
974
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
975 To execute the multicall, call the MultiCall object e.g.:
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
976
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
977 add_result, address = multicall()
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
978 """
30838
19bb1b2fb233 SF patch #834015: Remove imports of unused modules
Raymond Hettinger <python@rcn.com>
parents: 30822
diff changeset
979
30822
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
980 def __init__(self, server):
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
981 self.__server = server
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
982 self.__call_list = []
30838
19bb1b2fb233 SF patch #834015: Remove imports of unused modules
Raymond Hettinger <python@rcn.com>
parents: 30822
diff changeset
983
30822
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
984 def __repr__(self):
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
985 return "<MultiCall at %x>" % id(self)
30838
19bb1b2fb233 SF patch #834015: Remove imports of unused modules
Raymond Hettinger <python@rcn.com>
parents: 30822
diff changeset
986
30822
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
987 __str__ = __repr__
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
988
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
989 def __getattr__(self, name):
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
990 return _MultiCallMethod(self.__call_list, name)
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
991
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
992 def __call__(self):
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
993 marshalled_list = []
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
994 for name, args in self.__call_list:
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
995 marshalled_list.append({'methodName' : name, 'params' : args})
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
996
8cd691b73845 Patch #531629: Add multicall support.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
997 return MultiCallIterator(self.__server.system.multicall(marshalled_list))
30838
19bb1b2fb233 SF patch #834015: Remove imports of unused modules
Raymond Hettinger <python@rcn.com>
parents: 30822
diff changeset
998
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
999 # --------------------------------------------------------------------
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1000 # convenience functions
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1001
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1002 ##
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1003 # Create a parser object, and connect it to an unmarshalling instance.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1004 # This function picks the fastest available XML parser.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1005 #
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1006 # return A (parser, unmarshaller) tuple.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1007
34956
404088503a78 Add better datetime support to xmlrpclib module. Closes patch #1120353.
Skip Montanaro <skip@pobox.com>
parents: 34582
diff changeset
1008 def getparser(use_datetime=0):
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1009 """getparser() -> parser, unmarshaller
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1010
19840
377746a38972 more xmlrpclib tweaks: fixed repr(Fault()); enable UTF-8 parsing in
Fredrik Lundh <fredrik@pythonware.com>
parents: 19835
diff changeset
1011 Create an instance of the fastest available parser, and attach it
377746a38972 more xmlrpclib tweaks: fixed repr(Fault()); enable UTF-8 parsing in
Fredrik Lundh <fredrik@pythonware.com>
parents: 19835
diff changeset
1012 to an unmarshalling object. Return both objects.
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1013 """
34956
404088503a78 Add better datetime support to xmlrpclib module. Closes patch #1120353.
Skip Montanaro <skip@pobox.com>
parents: 34582
diff changeset
1014 if use_datetime and not datetime:
404088503a78 Add better datetime support to xmlrpclib module. Closes patch #1120353.
Skip Montanaro <skip@pobox.com>
parents: 34582
diff changeset
1015 raise ValueError, "the datetime module is not available"
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1016 if FastParser and FastUnmarshaller:
34956
404088503a78 Add better datetime support to xmlrpclib module. Closes patch #1120353.
Skip Montanaro <skip@pobox.com>
parents: 34582
diff changeset
1017 if use_datetime:
404088503a78 Add better datetime support to xmlrpclib module. Closes patch #1120353.
Skip Montanaro <skip@pobox.com>
parents: 34582
diff changeset
1018 mkdatetime = _datetime_type
404088503a78 Add better datetime support to xmlrpclib module. Closes patch #1120353.
Skip Montanaro <skip@pobox.com>
parents: 34582
diff changeset
1019 else:
404088503a78 Add better datetime support to xmlrpclib module. Closes patch #1120353.
Skip Montanaro <skip@pobox.com>
parents: 34582
diff changeset
1020 mkdatetime = _datetime
404088503a78 Add better datetime support to xmlrpclib module. Closes patch #1120353.
Skip Montanaro <skip@pobox.com>
parents: 34582
diff changeset
1021 target = FastUnmarshaller(True, False, _binary, mkdatetime, Fault)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1022 parser = FastParser(target)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1023 else:
34956
404088503a78 Add better datetime support to xmlrpclib module. Closes patch #1120353.
Skip Montanaro <skip@pobox.com>
parents: 34582
diff changeset
1024 target = Unmarshaller(use_datetime=use_datetime)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1025 if FastParser:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1026 parser = FastParser(target)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1027 elif ExpatParser:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1028 parser = ExpatParser(target)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1029 else:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1030 parser = SlowParser(target)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1031 return parser, target
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1032
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1033 ##
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1034 # Convert a Python tuple or a Fault instance to an XML-RPC packet.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1035 #
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1036 # @def dumps(params, **options)
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1037 # @param params A tuple or Fault instance.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1038 # @keyparam methodname If given, create a methodCall request for
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1039 # this method name.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1040 # @keyparam methodresponse If given, create a methodResponse packet.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1041 # If used with a tuple, the tuple must be a singleton (that is,
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1042 # it must contain exactly one element).
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1043 # @keyparam encoding The packet encoding.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1044 # @return A string containing marshalled data.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1045
28808
2ba20fc9f495 [Patch #628208] Add optional support for the 'nil' extension
Andrew M. Kuchling <amk@amk.ca>
parents: 28802
diff changeset
1046 def dumps(params, methodname=None, methodresponse=None, encoding=None,
2ba20fc9f495 [Patch #628208] Add optional support for the 'nil' extension
Andrew M. Kuchling <amk@amk.ca>
parents: 28802
diff changeset
1047 allow_none=0):
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1048 """data [,options] -> marshalled data
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1049
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1050 Convert an argument tuple or a Fault instance to an XML-RPC
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1051 request (or response, if the methodresponse option is used).
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1052
19840
377746a38972 more xmlrpclib tweaks: fixed repr(Fault()); enable UTF-8 parsing in
Fredrik Lundh <fredrik@pythonware.com>
parents: 19835
diff changeset
1053 In addition to the data object, the following options can be given
377746a38972 more xmlrpclib tweaks: fixed repr(Fault()); enable UTF-8 parsing in
Fredrik Lundh <fredrik@pythonware.com>
parents: 19835
diff changeset
1054 as keyword arguments:
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1055
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1056 methodname: the method name for a methodCall packet
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1057
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1058 methodresponse: true to create a methodResponse packet.
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1059 If this option is used with a tuple, the tuple must be
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1060 a singleton (i.e. it can contain only one element).
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1061
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1062 encoding: the packet encoding (default is UTF-8)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1063
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1064 All 8-bit strings in the data structure are assumed to use the
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1065 packet encoding. Unicode strings are automatically converted,
19840
377746a38972 more xmlrpclib tweaks: fixed repr(Fault()); enable UTF-8 parsing in
Fredrik Lundh <fredrik@pythonware.com>
parents: 19835
diff changeset
1066 where necessary.
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1067 """
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1068
34552
a24e70f4684b Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents: 34551
diff changeset
1069 assert isinstance(params, TupleType) or isinstance(params, Fault),\
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1070 "argument must be tuple or Fault instance"
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1071
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1072 if isinstance(params, Fault):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1073 methodresponse = 1
34552
a24e70f4684b Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents: 34551
diff changeset
1074 elif methodresponse and isinstance(params, TupleType):
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1075 assert len(params) == 1, "response tuple must be a singleton"
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1076
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1077 if not encoding:
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1078 encoding = "utf-8"
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1079
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1080 if FastMarshaller:
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1081 m = FastMarshaller(encoding)
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1082 else:
28808
2ba20fc9f495 [Patch #628208] Add optional support for the 'nil' extension
Andrew M. Kuchling <amk@amk.ca>
parents: 28802
diff changeset
1083 m = Marshaller(encoding, allow_none)
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1084
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1085 data = m.dumps(params)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1086
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1087 if encoding != "utf-8":
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1088 xmlheader = "<?xml version='1.0' encoding='%s'?>\n" % str(encoding)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1089 else:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1090 xmlheader = "<?xml version='1.0'?>\n" # utf-8 is default
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1091
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1092 # standard XML-RPC wrappings
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1093 if methodname:
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1094 # a method call
34552
a24e70f4684b Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents: 34551
diff changeset
1095 if not isinstance(methodname, StringType):
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1096 methodname = methodname.encode(encoding)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1097 data = (
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1098 xmlheader,
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1099 "<methodCall>\n"
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1100 "<methodName>", methodname, "</methodName>\n",
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1101 data,
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1102 "</methodCall>\n"
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1103 )
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1104 elif methodresponse:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1105 # a method response, or a fault structure
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1106 data = (
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1107 xmlheader,
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1108 "<methodResponse>\n",
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1109 data,
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1110 "</methodResponse>\n"
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1111 )
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1112 else:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1113 return data # return as is
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1114 return string.join(data, "")
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1115
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1116 ##
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1117 # Convert an XML-RPC packet to a Python object. If the XML-RPC packet
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1118 # represents a fault condition, this function raises a Fault exception.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1119 #
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1120 # @param data An XML-RPC packet, given as an 8-bit string.
30723
6f15e73b59f2 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 29825
diff changeset
1121 # @return A tuple containing the unpacked data, and the method name
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1122 # (None if not present).
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1123 # @see Fault
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1124
34956
404088503a78 Add better datetime support to xmlrpclib module. Closes patch #1120353.
Skip Montanaro <skip@pobox.com>
parents: 34582
diff changeset
1125 def loads(data, use_datetime=0):
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1126 """data -> unmarshalled data, method name
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1127
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1128 Convert an XML-RPC packet to unmarshalled data plus a method
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1129 name (None if not present).
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1130
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1131 If the XML-RPC packet represents a fault condition, this function
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1132 raises a Fault exception.
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1133 """
34956
404088503a78 Add better datetime support to xmlrpclib module. Closes patch #1120353.
Skip Montanaro <skip@pobox.com>
parents: 34582
diff changeset
1134 p, u = getparser(use_datetime=use_datetime)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1135 p.feed(data)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1136 p.close()
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1137 return u.close(), u.getmethodname()
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1138
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1139 ##
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1140 # Encode a string using the gzip content encoding such as specified by the
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1141 # Content-Encoding: gzip
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1142 # in the HTTP header, as described in RFC 1952
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1143 #
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1144 # @param data the unencoded data
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1145 # @return the encoded data
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1146
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1147 def gzip_encode(data):
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1148 """data -> gzip encoded data
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1149
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1150 Encode data using the gzip content encoding as described in RFC 1952
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1151 """
55343
3a613e00f66d http://bugs.python.org/issue6499
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 55089
diff changeset
1152 if not gzip:
3a613e00f66d http://bugs.python.org/issue6499
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 55089
diff changeset
1153 raise NotImplementedError
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1154 f = StringIO.StringIO()
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1155 gzf = gzip.GzipFile(mode="wb", fileobj=f, compresslevel=1)
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1156 gzf.write(data)
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1157 gzf.close()
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1158 encoded = f.getvalue()
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1159 f.close()
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1160 return encoded
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1161
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1162 ##
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1163 # Decode a string using the gzip content encoding such as specified by the
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1164 # Content-Encoding: gzip
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1165 # in the HTTP header, as described in RFC 1952
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1166 #
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1167 # @param data The encoded data
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1168 # @return the unencoded data
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1169 # @raises ValueError if data is not correctly coded.
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1170
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1171 def gzip_decode(data):
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1172 """gzip encoded data -> unencoded data
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1173
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1174 Decode data using the gzip content encoding as described in RFC 1952
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1175 """
55343
3a613e00f66d http://bugs.python.org/issue6499
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 55089
diff changeset
1176 if not gzip:
3a613e00f66d http://bugs.python.org/issue6499
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 55089
diff changeset
1177 raise NotImplementedError
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1178 f = StringIO.StringIO(data)
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1179 gzf = gzip.GzipFile(mode="rb", fileobj=f)
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1180 try:
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1181 decoded = gzf.read()
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1182 except IOError:
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1183 raise ValueError("invalid data")
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1184 f.close()
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1185 gzf.close()
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1186 return decoded
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1187
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1188 ##
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1189 # Return a decoded file-like object for the gzip encoding
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1190 # as described in RFC 1952.
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1191 #
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1192 # @param response A stream supporting a read() method
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1193 # @return a file-like object that the decoded data can be read() from
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1194
55345
eead7807df7c http://bugs.python.org/issue6499
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 55343
diff changeset
1195 class GzipDecodedResponse(gzip.GzipFile if gzip else object):
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1196 """a file-like object to decode a response encoded with the gzip
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1197 method, as described in RFC 1952.
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1198 """
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1199 def __init__(self, response):
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1200 #response doesn't support tell() and read(), required by
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1201 #GzipFile
55343
3a613e00f66d http://bugs.python.org/issue6499
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 55089
diff changeset
1202 if not gzip:
3a613e00f66d http://bugs.python.org/issue6499
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 55089
diff changeset
1203 raise NotImplementedError
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1204 self.stringio = StringIO.StringIO(response.read())
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1205 gzip.GzipFile.__init__(self, mode="rb", fileobj=self.stringio)
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1206
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1207 def close(self):
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1208 gzip.GzipFile.close(self)
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1209 self.stringio.close()
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1210
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1211
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1212 # --------------------------------------------------------------------
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1213 # request dispatcher
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1214
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1215 class _Method:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1216 # some magic to bind an XML-RPC method to an RPC server.
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1217 # supports "nested" methods (e.g. examples.getStateName)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1218 def __init__(self, send, name):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1219 self.__send = send
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1220 self.__name = name
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1221 def __getattr__(self, name):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1222 return _Method(self.__send, "%s.%s" % (self.__name, name))
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1223 def __call__(self, *args):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1224 return self.__send(self.__name, args)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1225
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1226 ##
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1227 # Standard transport class for XML-RPC over HTTP.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1228 # <p>
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1229 # You can create custom transports by subclassing this method, and
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1230 # overriding selected methods.
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1231
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1232 class Transport:
19835
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
1233 """Handles an HTTP transaction to an XML-RPC server."""
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1234
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1235 # client identifier (may be overridden)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1236 user_agent = "xmlrpclib.py/%s (by www.pythonware.com)" % __version__
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1237
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1238 #if true, we'll request gzip encoding
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1239 accept_gzip_encoding = True
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1240
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1241 # if positive, encode request using gzip if it exceeds this threshold
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1242 # note that many server will get confused, so only use it if you know
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1243 # that they can decode such a request
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1244 encode_threshold = None #None = don't encode
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1245
34956
404088503a78 Add better datetime support to xmlrpclib module. Closes patch #1120353.
Skip Montanaro <skip@pobox.com>
parents: 34582
diff changeset
1246 def __init__(self, use_datetime=0):
404088503a78 Add better datetime support to xmlrpclib module. Closes patch #1120353.
Skip Montanaro <skip@pobox.com>
parents: 34582
diff changeset
1247 self._use_datetime = use_datetime
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1248 self._connection = (None, None)
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1249 self._extra_headers = []
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1250 ##
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1251 # Send a complete request, and parse the response.
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1252 # Retry request if a cached connection has disconnected.
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1253 #
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1254 # @param host Target host.
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1255 # @param handler Target PRC handler.
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1256 # @param request_body XML-RPC request body.
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1257 # @param verbose Debugging flag.
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1258 # @return Parsed response.
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1259
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1260 def request(self, host, handler, request_body, verbose=0):
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1261 #retry request once if cached connection has gone cold
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1262 for i in (0, 1):
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1263 try:
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1264 return self.single_request(host, handler, request_body, verbose)
55088
8a7c2066a9b2 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54928
diff changeset
1265 except socket.error, e:
63195
c3866cd91ae0 Backport of 83120 (#9032)
Victor Stinner <victor.stinner@haypocalc.com>
parents: 55720
diff changeset
1266 if i or e.errno not in (errno.ECONNRESET, errno.ECONNABORTED, errno.EPIPE):
55088
8a7c2066a9b2 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54928
diff changeset
1267 raise
55089
4617883ac1f3 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 55088
diff changeset
1268 except httplib.BadStatusLine: #close after we sent request
55088
8a7c2066a9b2 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54928
diff changeset
1269 if i:
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1270 raise
34956
404088503a78 Add better datetime support to xmlrpclib module. Closes patch #1120353.
Skip Montanaro <skip@pobox.com>
parents: 34582
diff changeset
1271
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1272 ##
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1273 # Send a complete request, and parse the response.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1274 #
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1275 # @param host Target host.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1276 # @param handler Target PRC handler.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1277 # @param request_body XML-RPC request body.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1278 # @param verbose Debugging flag.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1279 # @return Parsed response.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1280
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1281 def single_request(self, host, handler, request_body, verbose=0):
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1282 # issue XML-RPC request
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1283
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1284 h = self.make_connection(host)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1285 if verbose:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1286 h.set_debuglevel(1)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1287
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1288 try:
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1289 self.send_request(h, handler, request_body)
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1290 self.send_host(h, host)
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1291 self.send_user_agent(h)
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1292 self.send_content(h, request_body)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1293
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1294 response = h.getresponse(buffering=True)
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1295 if response.status == 200:
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1296 self.verbose = verbose
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1297 return self.parse_response(response)
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1298 except Fault:
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1299 raise
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1300 except Exception:
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1301 # All unexpected errors leave connection in
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1302 # a strange state, so we clear it.
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1303 self.close()
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1304 raise
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1305
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1306 #discard any response data and raise exception
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1307 if (response.getheader("content-length", 0)):
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1308 response.read()
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1309 raise ProtocolError(
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1310 host + handler,
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1311 response.status, response.reason,
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1312 response.msg,
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1313 )
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1314
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1315 ##
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1316 # Create parser.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1317 #
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1318 # @return A 2-tuple containing a parser and a unmarshaller.
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1319
19835
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
1320 def getparser(self):
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
1321 # get parser and unmarshaller
34956
404088503a78 Add better datetime support to xmlrpclib module. Closes patch #1120353.
Skip Montanaro <skip@pobox.com>
parents: 34582
diff changeset
1322 return getparser(use_datetime=self._use_datetime)
19835
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
1323
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1324 ##
25882
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1325 # Get authorization info from host parameter
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1326 # Host may be a string, or a (host, x509-dict) tuple; if a string,
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1327 # it is checked for a "user:pw@host" format, and a "Basic
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1328 # Authentication" header is added if appropriate.
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1329 #
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1330 # @param host Host descriptor (URL or (URL, x509 info) tuple).
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1331 # @return A 3-tuple containing (actual host, extra headers,
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1332 # x509 info). The header and x509 fields may be None.
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1333
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1334 def get_host_info(self, host):
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1335
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1336 x509 = {}
34552
a24e70f4684b Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents: 34551
diff changeset
1337 if isinstance(host, TupleType):
25882
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1338 host, x509 = host
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1339
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1340 import urllib
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1341 auth, host = urllib.splituser(host)
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1342
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1343 if auth:
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1344 import base64
25981
06e005666c42 patch #624180 (part 2 of 2):
Fredrik Lundh <fredrik@pythonware.com>
parents: 25882
diff changeset
1345 auth = base64.encodestring(urllib.unquote(auth))
25882
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1346 auth = string.join(string.split(auth), "") # get rid of whitespace
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1347 extra_headers = [
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1348 ("Authorization", "Basic " + auth)
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1349 ]
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1350 else:
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1351 extra_headers = None
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1352
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1353 return host, extra_headers, x509
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1354
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1355 ##
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1356 # Connect to server.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1357 #
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1358 # @param host Target host.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1359 # @return A connection handle.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1360
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1361 def make_connection(self, host):
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1362 #return an existing connection if possible. This allows
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1363 #HTTP/1.1 keep-alive.
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1364 if self._connection and host == self._connection[0]:
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1365 return self._connection[1]
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1366
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1367 # create a HTTP connection object from a host descriptor
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1368 chost, self._extra_headers, x509 = self.get_host_info(host)
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1369 #store the host argument along with the connection object
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1370 self._connection = host, httplib.HTTPConnection(chost)
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1371 return self._connection[1]
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1372
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1373 ##
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1374 # Clear any cached connection object.
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1375 # Used in the event of socket errors.
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1376 #
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1377 def close(self):
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1378 if self._connection[1]:
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1379 self._connection[1].close()
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1380 self._connection = (None, None)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1381
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1382 ##
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1383 # Send request header.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1384 #
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1385 # @param connection Connection handle.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1386 # @param handler Target RPC handler.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1387 # @param request_body XML-RPC body.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1388
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1389 def send_request(self, connection, handler, request_body):
55343
3a613e00f66d http://bugs.python.org/issue6499
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 55089
diff changeset
1390 if (self.accept_gzip_encoding and gzip):
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1391 connection.putrequest("POST", handler, skip_accept_encoding=True)
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1392 connection.putheader("Accept-Encoding", "gzip")
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1393 else:
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1394 connection.putrequest("POST", handler)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1395
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1396 ##
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1397 # Send host name.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1398 #
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1399 # @param connection Connection handle.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1400 # @param host Host name.
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1401 #
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1402 # Note: This function doesn't actually add the "Host"
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1403 # header anymore, it is done as part of the connection.putrequest() in
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1404 # send_request() above.
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1405
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1406 def send_host(self, connection, host):
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1407 extra_headers = self._extra_headers
25882
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1408 if extra_headers:
34552
a24e70f4684b Revert previous checkin.
Raymond Hettinger <python@rcn.com>
parents: 34551
diff changeset
1409 if isinstance(extra_headers, DictType):
25882
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1410 extra_headers = extra_headers.items()
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1411 for key, value in extra_headers:
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1412 connection.putheader(key, value)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1413
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1414 ##
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1415 # Send user-agent identifier.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1416 #
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1417 # @param connection Connection handle.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1418
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1419 def send_user_agent(self, connection):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1420 connection.putheader("User-Agent", self.user_agent)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1421
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1422 ##
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1423 # Send request body.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1424 #
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1425 # @param connection Connection handle.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1426 # @param request_body XML-RPC request body.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1427
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1428 def send_content(self, connection, request_body):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1429 connection.putheader("Content-Type", "text/xml")
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1430
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1431 #optionally encode the request
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1432 if (self.encode_threshold is not None and
55343
3a613e00f66d http://bugs.python.org/issue6499
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 55089
diff changeset
1433 self.encode_threshold < len(request_body) and
3a613e00f66d http://bugs.python.org/issue6499
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 55089
diff changeset
1434 gzip):
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1435 connection.putheader("Content-Encoding", "gzip")
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1436 request_body = gzip_encode(request_body)
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1437
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1438 connection.putheader("Content-Length", str(len(request_body)))
49890
ef0c5ca45441 Send HTTP headers and message body in a single send() call.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 48160
diff changeset
1439 connection.endheaders(request_body)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1440
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1441 ##
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1442 # Parse response.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1443 #
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1444 # @param file Stream.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1445 # @return Response tuple and target method.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1446
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1447 def parse_response(self, response):
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1448 # read response data from httpresponse, and parse it
66773
6ef1aeae42be Fix Issue8194 - Fix incompatible API change in the parse_respones for xmlrpclib.
Senthil Kumaran <orsenthil@gmail.com>
parents: 63195
diff changeset
1449
6ef1aeae42be Fix Issue8194 - Fix incompatible API change in the parse_respones for xmlrpclib.
Senthil Kumaran <orsenthil@gmail.com>
parents: 63195
diff changeset
1450 # Check for new http response object, else it is a file object
6ef1aeae42be Fix Issue8194 - Fix incompatible API change in the parse_respones for xmlrpclib.
Senthil Kumaran <orsenthil@gmail.com>
parents: 63195
diff changeset
1451 if hasattr(response,'getheader'):
6ef1aeae42be Fix Issue8194 - Fix incompatible API change in the parse_respones for xmlrpclib.
Senthil Kumaran <orsenthil@gmail.com>
parents: 63195
diff changeset
1452 if response.getheader("Content-Encoding", "") == "gzip":
6ef1aeae42be Fix Issue8194 - Fix incompatible API change in the parse_respones for xmlrpclib.
Senthil Kumaran <orsenthil@gmail.com>
parents: 63195
diff changeset
1453 stream = GzipDecodedResponse(response)
6ef1aeae42be Fix Issue8194 - Fix incompatible API change in the parse_respones for xmlrpclib.
Senthil Kumaran <orsenthil@gmail.com>
parents: 63195
diff changeset
1454 else:
6ef1aeae42be Fix Issue8194 - Fix incompatible API change in the parse_respones for xmlrpclib.
Senthil Kumaran <orsenthil@gmail.com>
parents: 63195
diff changeset
1455 stream = response
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1456 else:
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1457 stream = response
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1458
19835
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
1459 p, u = self.getparser()
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1460
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1461 while 1:
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1462 data = stream.read(1024)
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1463 if not data:
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1464 break
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1465 if self.verbose:
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1466 print "body:", repr(data)
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1467 p.feed(data)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1468
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1469 if stream is not response:
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1470 stream.close()
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1471 p.close()
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1472
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1473 return u.close()
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1474
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1475 ##
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1476 # Standard transport class for XML-RPC over HTTPS.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1477
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1478 class SafeTransport(Transport):
19835
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
1479 """Handles an HTTPS transaction to an XML-RPC server."""
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1480
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1481 # FIXME: mostly untested
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1482
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1483 def make_connection(self, host):
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1484 if self._connection and host == self._connection[0]:
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1485 return self._connection[1]
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1486 # create a HTTPS connection object from a host descriptor
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1487 # host may be a string, or a (host, x509-dict) tuple
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1488 try:
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1489 HTTPS = httplib.HTTPSConnection
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1490 except AttributeError:
25882
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1491 raise NotImplementedError(
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1492 "your version of httplib doesn't support HTTPS"
6f18508bed3d add support for basic authentication, based on patch #624180
Fredrik Lundh <fredrik@pythonware.com>
parents: 24058
diff changeset
1493 )
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1494 else:
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1495 chost, self._extra_headers, x509 = self.get_host_info(host)
55720
c4d8cbafd8c8 issue 6769
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 55345
diff changeset
1496 self._connection = host, HTTPS(chost, None, **(x509 or {}))
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1497 return self._connection[1]
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1498
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1499 ##
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1500 # Standard server proxy. This class establishes a virtual connection
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1501 # to an XML-RPC server.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1502 # <p>
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1503 # This class is available as ServerProxy and Server. New code should
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1504 # use ServerProxy, to avoid confusion.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1505 #
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1506 # @def ServerProxy(uri, **options)
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1507 # @param uri The connection point on the server.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1508 # @keyparam transport A transport factory, compatible with the
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1509 # standard transport class.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1510 # @keyparam encoding The default encoding used for 8-bit strings
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1511 # (default is UTF-8).
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1512 # @keyparam verbose Use a true value to enable debugging output.
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1513 # (printed to standard output).
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1514 # @see Transport
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1515
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1516 class ServerProxy:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1517 """uri [,options] -> a logical connection to an XML-RPC server
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1518
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1519 uri is the connection point on the server, given as
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1520 scheme://host/target.
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1521
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1522 The standard implementation always supports the "http" scheme. If
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1523 SSL socket support is available (Python 2.0), it also supports
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1524 "https".
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1525
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1526 If the target part and the slash preceding it are both omitted,
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1527 "/RPC2" is assumed.
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1528
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1529 The following options can be given as keyword arguments:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1530
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1531 transport: a transport factory
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1532 encoding: the request encoding (default is UTF-8)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1533
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1534 All 8-bit strings passed to the server proxy are assumed to use
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1535 the given encoding.
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1536 """
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1537
28808
2ba20fc9f495 [Patch #628208] Add optional support for the 'nil' extension
Andrew M. Kuchling <amk@amk.ca>
parents: 28802
diff changeset
1538 def __init__(self, uri, transport=None, encoding=None, verbose=0,
34956
404088503a78 Add better datetime support to xmlrpclib module. Closes patch #1120353.
Skip Montanaro <skip@pobox.com>
parents: 34582
diff changeset
1539 allow_none=0, use_datetime=0):
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1540 # establish a "logical" server connection
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1541
72450
c02e790c4535 Issue #12931: xmlrpclib now encodes Unicode URI to ISO-8859-1, instead of
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66773
diff changeset
1542 if isinstance(uri, unicode):
c02e790c4535 Issue #12931: xmlrpclib now encodes Unicode URI to ISO-8859-1, instead of
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66773
diff changeset
1543 uri = uri.encode('ISO-8859-1')
c02e790c4535 Issue #12931: xmlrpclib now encodes Unicode URI to ISO-8859-1, instead of
Victor Stinner <victor.stinner@haypocalc.com>
parents: 66773
diff changeset
1544
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1545 # get the url
19835
9cf1338fd8d9 sync with pythonware codebase: much faster import (doesn't import
Fredrik Lundh <fredrik@pythonware.com>
parents: 19691
diff changeset
1546 import urllib
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1547 type, uri = urllib.splittype(uri)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1548 if type not in ("http", "https"):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1549 raise IOError, "unsupported XML-RPC protocol"
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1550 self.__host, self.__handler = urllib.splithost(uri)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1551 if not self.__handler:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1552 self.__handler = "/RPC2"
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1553
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1554 if transport is None:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1555 if type == "https":
34956
404088503a78 Add better datetime support to xmlrpclib module. Closes patch #1120353.
Skip Montanaro <skip@pobox.com>
parents: 34582
diff changeset
1556 transport = SafeTransport(use_datetime=use_datetime)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1557 else:
34956
404088503a78 Add better datetime support to xmlrpclib module. Closes patch #1120353.
Skip Montanaro <skip@pobox.com>
parents: 34582
diff changeset
1558 transport = Transport(use_datetime=use_datetime)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1559 self.__transport = transport
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1560
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1561 self.__encoding = encoding
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1562 self.__verbose = verbose
28808
2ba20fc9f495 [Patch #628208] Add optional support for the 'nil' extension
Andrew M. Kuchling <amk@amk.ca>
parents: 28802
diff changeset
1563 self.__allow_none = allow_none
29087
526f37fb380d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 28808
diff changeset
1564
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1565 def __close(self):
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1566 self.__transport.close()
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1567
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1568 def __request(self, methodname, params):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1569 # call a method on the remote server
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1570
28808
2ba20fc9f495 [Patch #628208] Add optional support for the 'nil' extension
Andrew M. Kuchling <amk@amk.ca>
parents: 28802
diff changeset
1571 request = dumps(params, methodname, encoding=self.__encoding,
2ba20fc9f495 [Patch #628208] Add optional support for the 'nil' extension
Andrew M. Kuchling <amk@amk.ca>
parents: 28802
diff changeset
1572 allow_none=self.__allow_none)
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1573
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1574 response = self.__transport.request(
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1575 self.__host,
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1576 self.__handler,
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1577 request,
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1578 verbose=self.__verbose
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1579 )
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1580
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1581 if len(response) == 1:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1582 response = response[0]
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1583
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1584 return response
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1585
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1586 def __repr__(self):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1587 return (
19450
cee1decc4ff2 updated to current PythonWare version (1.0b3). fixed type checks in
Fredrik Lundh <fredrik@pythonware.com>
parents: 18477
diff changeset
1588 "<ServerProxy for %s%s>" %
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1589 (self.__host, self.__handler)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1590 )
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1591
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1592 __str__ = __repr__
30838
19bb1b2fb233 SF patch #834015: Remove imports of unused modules
Raymond Hettinger <python@rcn.com>
parents: 30822
diff changeset
1593
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1594 def __getattr__(self, name):
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1595 # magic method dispatcher
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1596 return _Method(self.__request, name)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1597
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1598 # note: to call a remote object with an non-standard name, use
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1599 # result getattr(server, "strange-python-name")(args)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1600
54928
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1601 def __call__(self, attr):
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1602 """A workaround to get special attributes on the ServerProxy
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1603 without interfering with the magic __getattr__
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1604 """
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1605 if attr == "close":
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1606 return self.__close
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1607 elif attr == "transport":
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1608 return self.__transport
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1609 raise AttributeError("Attribute %r not found" % (attr,))
ffd7cf5c7285 http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 54561
diff changeset
1610
19450
cee1decc4ff2 updated to current PythonWare version (1.0b3). fixed type checks in
Fredrik Lundh <fredrik@pythonware.com>
parents: 18477
diff changeset
1611 # compatibility
24031
d10f1c6d9ee9 merged with SLAB codebase (version 1.0.1)
Fredrik Lundh <fredrik@pythonware.com>
parents: 23628
diff changeset
1612
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1613 Server = ServerProxy
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1614
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1615 # --------------------------------------------------------------------
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1616 # test code
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1617
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1618 if __name__ == "__main__":
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1619
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1620 # simple test program (from the XML-RPC specification)
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1621
19450
cee1decc4ff2 updated to current PythonWare version (1.0b3). fixed type checks in
Fredrik Lundh <fredrik@pythonware.com>
parents: 18477
diff changeset
1622 # server = ServerProxy("http://localhost:8000") # local server
33223
8abd322a013a Replace yield with sequence class. Fixes #1009803.
Martin v. Löwis <martin@v.loewis.de>
parents: 33209
diff changeset
1623 server = ServerProxy("http://time.xmlrpc.com/RPC2")
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1624
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1625 print server
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1626
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1627 try:
33223
8abd322a013a Replace yield with sequence class. Fixes #1009803.
Martin v. Löwis <martin@v.loewis.de>
parents: 33209
diff changeset
1628 print server.currentTime.getCurrentTime()
18477
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1629 except Error, v:
c397d4cd837d xmlrpclib for python 2.2; initial checkin
Fredrik Lundh <fredrik@pythonware.com>
parents:
diff changeset
1630 print "ERROR", v
33223
8abd322a013a Replace yield with sequence class. Fixes #1009803.
Martin v. Löwis <martin@v.loewis.de>
parents: 33209
diff changeset
1631
8abd322a013a Replace yield with sequence class. Fixes #1009803.
Martin v. Löwis <martin@v.loewis.de>
parents: 33209
diff changeset
1632 multi = MultiCall(server)
8abd322a013a Replace yield with sequence class. Fixes #1009803.
Martin v. Löwis <martin@v.loewis.de>
parents: 33209
diff changeset
1633 multi.currentTime.getCurrentTime()
8abd322a013a Replace yield with sequence class. Fixes #1009803.
Martin v. Löwis <martin@v.loewis.de>
parents: 33209
diff changeset
1634 multi.currentTime.getCurrentTime()
8abd322a013a Replace yield with sequence class. Fixes #1009803.
Martin v. Löwis <martin@v.loewis.de>
parents: 33209
diff changeset
1635 try:
8abd322a013a Replace yield with sequence class. Fixes #1009803.
Martin v. Löwis <martin@v.loewis.de>
parents: 33209
diff changeset
1636 for response in multi():
8abd322a013a Replace yield with sequence class. Fixes #1009803.
Martin v. Löwis <martin@v.loewis.de>
parents: 33209
diff changeset
1637 print response
8abd322a013a Replace yield with sequence class. Fixes #1009803.
Martin v. Löwis <martin@v.loewis.de>
parents: 33209
diff changeset
1638 except Error, v:
8abd322a013a Replace yield with sequence class. Fixes #1009803.
Martin v. Löwis <martin@v.loewis.de>
parents: 33209
diff changeset
1639 print "ERROR", v