Package rrlog :: Package server :: Module xmlrpc
[hide private]
[frames] | no frames]

Source Code for Module rrlog.server.xmlrpc

  1  #!/usr/bin/python 
  2   
  3  #Copyright (c) 2007 
  4  #        Ruben Reifenberg, Germany, 07381. 
  5  #    All rights reserved. 
  6  # 
  7  #Redistribution and use in source and binary forms, with or without 
  8  #modification, are permitted provided that the following conditions 
  9  #are met: 
 10  #1. Redistributions of source code must retain the above copyright 
 11  #   notice, this list of conditions and the following disclaimer as 
 12  #   the first lines of this file unmodified. 
 13  #2. Redistributions in binary form must reproduce the above copyright 
 14  #   notice, this list of conditions and the following disclaimer in the 
 15  #   documentation and/or other materials provided with the distribution. 
 16  # 
 17  # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 
 18  # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 19  # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 20  # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 
 21  # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
 22  # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
 23  # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 24  # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 25  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
 26  # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
 27  # SUCH DAMAGE. 
 28   
 29  """ 
 30  @summary: 
 31  The XML-RPC version of a LogServer connection. 
 32  This is a slow way to log 
 33  (mainly because of the unnecessary overhead of 
 34  xml-rpc blocking calls), but simple and robust. 
 35  Use it when the performance is good enough for you. 
 36  @author: Ruben Reifenberg 
 37  """ 
 38   
 39  from SimpleXMLRPCServer import SimpleXMLRPCServer 
 40  import pickle 
 41   
 42   
 43   
44 -class LogAdapter(object):
45 """ 46 """
47 - def __init__(self, logServer):
48 """ 49 @return: "" if all right, error msg if not 50 """ 51 self.s = logServer
52 53 # def log(self, pid, clientid, msgid, msg, specialPS, cat, pathPS, tblen, cfunc): <=0.2.1
54 - def log(self, kwargs_ps): # 0.2.2: single arg as for non-xmlrpc version
55 """ 56 No threading, xmlrpc blocks until return 57 """ 58 try: 59 kwargs = pickle.loads(kwargs_ps.data) 60 except Exception,e: 61 return "invalid pickle data:"+str(e) 62 if kwargs["special"] is None: kwargs["special"] = {} 63 try: 64 # self.s.log(pid, clientid, msgid, msg, special, cat, path, tblen, cfunc)<=0.2.1 65 self.s.log(**kwargs) 66 except Exception,e: 67 return "log failed:"+str(e) 68 return ""
69 70 71 # try: 72 # special = pickle.loads(specialPS.data) 73 # except Exception,e: 74 # return "invalid special:"+str(e) 75 # if pathPS != "": 76 # try: 77 # path = pickle.loads(pathPS.data) 78 # except Exception,e: 79 # return "invalid path:"+str(e) 80 # else: 81 # path = () 82 # try: 83 # self.s.log(pid, clientid, msgid, msg, special, cat, path, tblen, cfunc) 84 # except Exception,e: 85 # return "log failed:"+str(e) 86 # return "" 87
88 - def addClient(self):
89 """ 90 @rtype: int 91 @return: unique id 92 """ 93 return self.s.addClient()
94 95 96
97 -def createSimpleXMLRPCServer(host, ports):
98 """ 99 Creates server that does no multithreading. 100 Remark: The SimpleXMLRPCServer seems to do no multithreading, 101 except we use MixIn class. 102 Indeed, I found that a second client 103 is blocked as long as the server is processing another request. 104 (Python 2.4) 105 @param ports: list or iterator of port numbers 106 Multiple ports address the problem that a socket is for some time 107 "already in use" when restarting the server. 108 The first free port of the ports is used. 109 """ 110 for port in ports: 111 try: 112 res = SimpleXMLRPCServer((host,port)) 113 except Exception, e: 114 print "Retrying:%s"%(e) 115 else: 116 res.logRequests = 0 # hint from newsgroup to disable the - - [13/Apr/2007 13:33:57] "POST /RPC2 HTTP/1.0" 200 - output with every request 117 return res
118 119 120
121 -def startServer(logServer,ports,host="localhost",readyMsg=True):
122 """ 123 Run the given logServer as an xmlrpc server (forever). 124 @param ports: see L{createSimpleXMLRPCServer} 125 """ 126 server = createSimpleXMLRPCServer(host,ports) 127 adapter = LogAdapter(logServer) 128 server.register_function(adapter.log, "log") 129 server.register_function(adapter.addClient, "addClient") 130 if readyMsg: print "log server ready. Available at host,port: %s"%(str(server.server_address)) 131 server.serve_forever()
132