XMLRPCServlet.py :  » Web-Frameworks » Webware » Webware-1.0.2 » WebKit » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Webware 
Webware » Webware 1.0.2 » WebKit » XMLRPCServlet.py
"""XML-RPC servlet base class

Written by Geoffrey Talvola

This requires the xml-rpc library available from
http://www.pythonware.com/products/xmlrpc/
which was was added to the standard Python library in Python 2.2.

See Examples/XMLRPCExample.py for sample usage.

"""

try:
  # may be a part of a package in Python < 2.2, check this first
  from xmlrpclib import xmlrpclib
except ImportError:
  import xmlrpclib

import sys, traceback

try: # for Python < 2.3
  True, False
except NameError:
  True, False = 1, 0

from RPCServlet import RPCServlet


class XMLRPCServlet(RPCServlet):
  """XMLRPCServlet is a base class for XML-RPC servlets.

  See Examples/XMLRPCExample.py for sample usage.

  For more Pythonic convenience at the cost of language independence,
  see PickleRPCServlet.

  """

  # Set to False if you do not want to allow None to be marshalled
  # as part of a response.
  allow_none = True

  def respondToPost(self, transaction):
    """
    This is similar to the xmlrpcserver.py example from the xmlrpc
    library distribution, only it's been adapted to work within a
    WebKit servlet.
    """
    try:
      # get arguments
      data = transaction.request().rawInput(rewind=1).read()
      encoding = _getXmlDeclAttr(data, "encoding")
      params, method = xmlrpclib.loads(data)

      # generate response
      try:
        # This first test helps us to support PythonWin, which uses
        # repeated calls to __methods__.__getitem__ to determine the
        # allowed methods of an object.
        if method == '__methods__.__getitem__':
          response = self.exposedMethods()[params[0]]
        else:
          response = self.call(method, *params)
        if type(response) != type(()):
          response = (response,)
      except xmlrpclib.Fault, fault:
        response = xmlrpclib.dumps(fault, encoding=encoding,
          allow_none=self.allow_none)
        self.sendOK('text/xml', response, transaction)
        self.handleException(transaction)
      except Exception, e:
        fault = self.resultForException(e, transaction)
        response = xmlrpclib.dumps(xmlrpclib.Fault(1, fault),
          encoding=encoding, allow_none=self.allow_none)
        self.sendOK('text/xml', response, transaction)
        self.handleException(transaction)
      except:  # if it's a string exception, this gets triggered
        fault = self.resultForException(sys.exc_info()[0], transaction)
        response = xmlrpclib.dumps(xmlrpclib.Fault(1, fault),
          encoding=encoding, allow_none=self.allow_none)
        self.sendOK('text/xml', response, transaction)
        self.handleException(transaction)
      else:
        response = xmlrpclib.dumps(response, methodresponse=1,
          encoding=encoding, allow_none=self.allow_none)
        self.sendOK('text/xml', response, transaction)
    except Exception:
      # internal error, report as HTTP server error
      print 'XMLRPCServlet internal error'
      print ''.join(traceback.format_exception(
        sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))
      transaction.response().setStatus(500, 'Server Error')
      self.handleException(transaction)


# Helper functions:

def _getXmlDeclAttr(xml, attName):
  """Get attribute value from xml declaration (<?xml ... ?>)."""
  s = xml[6 : xml.find("?>")] # 'version = "1.0" encoding = "Cp1251"'
  p = s.find(attName)
  if p == -1:
    return None
  s = s[p + len(attName):] # '= "Cp1251"'
  s = s[s.find('=') + 1:].strip() # '"Cp1251"'
  return s[1:s.find(s[0], 1)] # 'Cp1251'
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.