diff options
author | Fredrik Lundh <fredrik@pythonware.com> | 2001-07-11 17:42:21 (GMT) |
---|---|---|
committer | Fredrik Lundh <fredrik@pythonware.com> | 2001-07-11 17:42:21 (GMT) |
commit | b905633be016fabfee2290cb34d4a9c9fb5fe517 (patch) | |
tree | 54fb10889b22d2dda8c9c20bd96fd4350f985caf /Demo | |
parent | c2c12dc31cfbe1babe3e8f7f6a4f5bc709e684da (diff) | |
download | cpython-b905633be016fabfee2290cb34d4a9c9fb5fe517.zip cpython-b905633be016fabfee2290cb34d4a9c9fb5fe517.tar.gz cpython-b905633be016fabfee2290cb34d4a9c9fb5fe517.tar.bz2 |
xmlrpclib for python 2.2; initial checkin
Diffstat (limited to 'Demo')
-rw-r--r-- | Demo/xmlrpc/xmlrpc_handler.py | 106 | ||||
-rw-r--r-- | Demo/xmlrpc/xmlrpcserver.py | 75 |
2 files changed, 181 insertions, 0 deletions
diff --git a/Demo/xmlrpc/xmlrpc_handler.py b/Demo/xmlrpc/xmlrpc_handler.py new file mode 100644 index 0000000..9fa1766 --- /dev/null +++ b/Demo/xmlrpc/xmlrpc_handler.py @@ -0,0 +1,106 @@ +# +# XML-RPC SERVER +# $Id$ +# +# an asynchronous XML-RPC server for Medusa +# +# written by Sam Rushing +# +# Based on "xmlrpcserver.py" by Fredrik Lundh (fredrik@pythonware.com) +# + +import http_server +import xmlrpclib + +import regex +import string +import sys + +class xmlrpc_handler: + + def match (self, request): + # Note: /RPC2 is not required by the spec, so you may override this method. + if request.uri[:5] == '/RPC2': + return 1 + else: + return 0 + + def handle_request (self, request): + [path, params, query, fragment] = request.split_uri() + + if request.command in ('post', 'put'): + request.collector = collector (self, request) + else: + request.error (400) + + def continue_request (self, data, request): + params, method = xmlrpclib.loads (data) + try: + # generate response + try: + response = self.call (method, params) + response = (response,) + except: + # report exception back to server + response = xmlrpclib.dumps ( + xmlrpclib.Fault (1, "%s:%s" % (sys.exc_type, sys.exc_value)) + ) + else: + response = xmlrpclib.dumps (response, methodresponse=1) + except: + # internal error, report as HTTP server error + request.error (500) + else: + # got a valid XML RPC response + request['Content-Type'] = 'text/xml' + request.push (response) + request.done() + + def call (self, method, params): + # override this method to implement RPC methods + raise "NotYetImplemented" + +class collector: + + "gathers input for POST and PUT requests" + + def __init__ (self, handler, request): + + self.handler = handler + self.request = request + self.data = '' + + # make sure there's a content-length header + cl = request.get_header ('content-length') + + if not cl: + request.error (411) + else: + cl = string.atoi (cl) + # using a 'numeric' terminator + self.request.channel.set_terminator (cl) + + def collect_incoming_data (self, data): + self.data = self.data + data + + def found_terminator (self): + # set the terminator back to the default + self.request.channel.set_terminator ('\r\n\r\n') + self.handler.continue_request (self.data, self.request) + +if __name__ == '__main__': + + class rpc_demo (xmlrpc_handler): + + def call (self, method, params): + print 'method="%s" params=%s' % (method, params) + return "Sure, that works" + + import asyncore + import http_server + + hs = http_server.http_server ('', 8000) + rpc = rpc_demo() + hs.install_handler (rpc) + + asyncore.loop() diff --git a/Demo/xmlrpc/xmlrpcserver.py b/Demo/xmlrpc/xmlrpcserver.py new file mode 100644 index 0000000..fa30419 --- /dev/null +++ b/Demo/xmlrpc/xmlrpcserver.py @@ -0,0 +1,75 @@ +# +# XML-RPC SERVER +# $Id$ +# +# a simple XML-RPC server for Python +# +# History: +# 1999-02-01 fl added to xmlrpclib distribution +# +# written by Fredrik Lundh, January 1999. +# +# Copyright (c) 1999 by Secret Labs AB. +# Copyright (c) 1999 by Fredrik Lundh. +# +# fredrik@pythonware.com +# http://www.pythonware.com +# +# -------------------------------------------------------------------- +# Permission to use, copy, modify, and distribute this software and +# its associated documentation for any purpose and without fee is +# hereby granted. This software is provided as is. +# -------------------------------------------------------------------- +# + +import SocketServer, BaseHTTPServer +import xmlrpclib +import sys + +class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): + + def do_POST(self): + try: + # get arguments + data = self.rfile.read(int(self.headers["content-length"])) + params, method = xmlrpclib.loads(data) + + # generate response + try: + response = self.call(method, params) + # wrap response in a singleton tuple + response = (response,) + except: + # report exception back to server + response = xmlrpclib.dumps( + xmlrpclib.Fault(1, "%s:%s" % (sys.exc_type, sys.exc_value)) + ) + else: + response = xmlrpclib.dumps( + response, + methodresponse=1 + ) + except: + # internal error, report as HTTP server error + self.send_response(500) + self.end_headers() + else: + # got a valid XML RPC response + self.send_response(200) + self.send_header("Content-type", "text/xml") + self.send_header("Content-length", str(len(response))) + self.end_headers() + self.wfile.write(response) + + # shut down the connection (from Skip Montanaro) + self.wfile.flush() + self.connection.shutdown(1) + + def call(self, method, params): + # override this method to implement RPC methods + print "CALL", method, params + return params + +if __name__ == '__main__': + server = SocketServer.TCPServer(('', 8000), RequestHandler) + server.serve_forever() |