summaryrefslogtreecommitdiff
path: root/py3/cherrypy/lib/xmlrpc.py
diff options
context:
space:
mode:
authorRobert Brewer <fumanchu@aminus.org>2011-02-25 22:38:17 +0000
committerRobert Brewer <fumanchu@aminus.org>2011-02-25 22:38:17 +0000
commitfd8b1e2a2662d965f05a14c2c72701decd81697c (patch)
treef6b7f89ffb181bc940c7d9b3541f01f25b49699d /py3/cherrypy/lib/xmlrpc.py
parent5450bb603f7e0cf91d216225d3fa3f524710bcd6 (diff)
downloadcherrypy-git-trunk.tar.gz
Bringing python3 back into trunk with its own py3 folder; what was in trunk goes in the py2 folder, and we can use a single setup.py to install either, depending on the version of Python that runs setup.py install.trunk
Diffstat (limited to 'py3/cherrypy/lib/xmlrpc.py')
-rw-r--r--py3/cherrypy/lib/xmlrpc.py49
1 files changed, 49 insertions, 0 deletions
diff --git a/py3/cherrypy/lib/xmlrpc.py b/py3/cherrypy/lib/xmlrpc.py
new file mode 100644
index 00000000..402b0a2e
--- /dev/null
+++ b/py3/cherrypy/lib/xmlrpc.py
@@ -0,0 +1,49 @@
+import sys
+
+import cherrypy
+
+
+def process_body():
+ """Return (params, method) from request body."""
+ try:
+ from xmlrpc import client
+ return client.loads(cherrypy.request.body.read())
+ except Exception:
+ return ('ERROR PARAMS', ), 'ERRORMETHOD'
+
+
+def patched_path(path):
+ """Return 'path', doctored for RPC."""
+ if not path.endswith('/'):
+ path += '/'
+ if path.startswith('/RPC2/'):
+ # strip the first /rpc2
+ path = path[5:]
+ return path
+
+
+def _set_response(body):
+ # The XML-RPC spec (http://www.xmlrpc.com/spec) says:
+ # "Unless there's a lower-level error, always return 200 OK."
+ # Since Python's xmlrpclib interprets a non-200 response
+ # as a "Protocol Error", we'll just return 200 every time.
+ response = cherrypy.response
+ response.status = '200 OK'
+ response.body = bytes(body, 'utf-8')
+ response.headers['Content-Type'] = 'text/xml'
+ response.headers['Content-Length'] = len(body)
+
+
+def respond(body, encoding='utf-8', allow_none=0):
+ from xmlrpc.client import Fault, dumps
+ if not isinstance(body, Fault):
+ body = (body,)
+ _set_response(dumps(body, methodresponse=1,
+ encoding=encoding,
+ allow_none=allow_none))
+
+def on_error(*args, **kwargs):
+ body = str(sys.exc_info()[1])
+ from xmlrpc.client import Fault, dumps
+ _set_response(dumps(Fault(1, body)))
+