problem getting xmlrpclib example to work

Tim Lynch tjl3 at cornell.edu
Fri Mar 29 12:06:55 EST 2002


Could someone get me pointed in the right direction?

I've been banging my head for over a day now trying to get an example
xml-rpc
cgi-based server to work.  Specifically, I'm trying to get the example
from
O'Reilly's "XML-RPC Web Services" book, pg 137, ex. 6-3, to work.  I've
copied the code from www.ora.com for ex 6-3 to my cgi-bin directory,
it's
included at the end of this message.

I've tried the code on several different Linux installations, running
Python 2.1.1
and 2.2 all with the same results.


Here's the test script to exercise the cgi script:
----
#!/usr/bin/python

import xmlrpclib
server = xmlrpclib.Server('http://localhost/cgi-bin/xmlrpc.py')

print server.Describe('xxx')
----

And here's the traceback I get:

Traceback (most recent call last):
  File "./tst.py", line 6, in ?
    print server.Describe('xxx')
  File "/usr/local/lib/python2.1/xmlrpclib.py", line 547, in __call__
    return self.__send(self.__name, args)
  File "/usr/local/lib/python2.1/xmlrpclib.py", line 630, in __request
    request
  File "/usr/local/lib/python2.1/xmlrpclib.py", line 582, in request
    headers
xmlrpclib.ProtocolError: <ProtocolError for localhost/cgi-bin/xmlrpc.py:
500 Internal Server Error>


Here is the cgi code:

----
#!/usr/bin/python

# xmlrpc_cgi.py

import xmlrpclib
import os, sys, string

#### HTTP message templates

success_msg = """
HTTP/1.1 200 OK
Content-type: text/xml
Content-length: %d

%s
"""

failure_msg = """
HTTP/1.1 500 Internal Server Error - x.py

"""

#### functions that implement XML-RPC API

def do_SetColor(shape_type, shape_id, color_tuple):
  return "hello from do_SetColor"

def do_Describe(*one_or_more_args):
  return "hello from do_Describe"

def call(meth_name, arg_tuple):
   """
   XML-RPC method dispatcher
   """
   myAPI = ("SetColor", "Bounce", "Describe")
   if meth_name in myAPI:
      func_obj = eval("do_" + meth_name)
      return apply(func_obj, arg_tuple)
   else:
      raise xmlrpclib.Fault(123, "Unknown method name")

##### main program

if __name__ == "__main__":

   try:
      # get arguments
      data = sys.stdin.read(  )
      fh = open("/tmp/xmlrpc.cgi.log", 'w')
      fh.write(data)
      fh.close()
      params, method = xmlrpclib.loads(data)

      # generate response
      try:
         response = call(method, params)
         if type(response) != type((  )):
            response = (response,)

      except xmlrpclib.Fault, faultobj:
         # generated response was a Fault
         response = xmlrpclib.dumps(faultobj)

      except:
         # report exception back to server
         response = xmlrpclib.dumps(
           xmlrpclib.Fault(1,
           "%s:%s" % (sys.exc_type, sys.exc_value))
         )
      else:
         # no exception occurred:
         # send method's return value back to server
         response = xmlrpclib.dumps(
            response,
            methodresponse=1
            )
   except:
      # internal error, report as HTTP server error
      print failure_msg
   else:
      # no internal error: send back a "success" response
      print success_msg % (len(XML_response), XML_response)
----

What's really annoying is that I've got equivalent Perl code
working on the same machines I've tested this code.

Tim Lynch
Mann Library
Cornell University
Ithaca, NY 14853






More information about the Python-list mailing list