1 #
 2 # XMLRPC SERVER
 3 # $Id: xmlrpcserver.py 12937 2004-11-24 20:15:11Z ruff $
 4 #
 5 # a simple XMLRPC server for Python
 6 #
 7 # History:
 8 # 1999-02-01 fl  added to xmlrpclib distribution 
 9 #
10 # written by Fredrik Lundh, January 1999.
11 #
12 # Copyright (c) 1999 by Secret Labs AB.
13 # Copyright (c) 1999 by Fredrik Lundh.
14 #
15 # fredrik@pythonware.com
16 # http://www.pythonware.com
17 #
18 # --------------------------------------------------------------------
19 # Permission to use, copy, modify, and distribute this software and
20 # its associated documentation for any purpose and without fee is
21 # hereby granted.  This software is provided as is.
22 # --------------------------------------------------------------------
23 #
24 
25 import SocketServer, BaseHTTPServer
26 import xmlrpclib
27 import sys
28 
29 class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
30 
31     def do_POST(self):
32         try:
33        # get arguments
34             data = self.rfile.read(int(self.headers["content-length"]))
35             params, method = xmlrpclib.loads(data)
36 
37        # generate response
38        try:
39       response = self.call(method, params)
40       if type(response) != type(()):
41           response = (response,)
42        except:
43       # report exception back to server
44       response = xmlrpclib.dumps(
45           xmlrpclib.Fault(1, "%s:%s" % (sys.exc_type, sys.exc_value))
46           )
47        else:
48       response = xmlrpclib.dumps(
49           response,
50           methodresponse=1
51           )
52         except:
53        # internal error, report as HTTP server error
54        self.send_response(500)
55        self.end_headers()
56    else:
57        # got a valid XML RPC response
58        self.send_response(200)
59        self.send_header("Content-type", "text/xml")
60        self.send_header("Content-length", str(len(response)))
61        self.end_headers()
62        self.wfile.write(response)
63 
64        # shut down the connection (from Skip Montanaro)
65        self.wfile.flush()
66        self.connection.shutdown(1)
67 
68     def call(self, method, params):
69    # override this method to implement RPC methods
70    print "CALL", method, params
71    return params
72 
73 if __name__ == '__main__':
74     server = SocketServer.TCPServer(('', 8000), RequestHandler)
75     server.serve_forever()


syntax highlighted by Code2HTML, v. 0.9.1