XMLRPC Server Slow in Python – How to Fix

Background:
I had set up a Python XML-RPC server on one machine. When accessing the web services from other machines it would sometimes take up to 20 seconds to get a response. The strange thing was that this only happened when accessing the web service from some machines but not others.

Problem and Solution:
It turns out that Python’s BaseHTTPRequestHandler was trying to log the fully qualified domain name of each request’s IP address. Thus when we connected from machines that didn’t have a fully qualified domain name, it would take a long time to not find the FQDN. There is actually a bug for this.

Below is a simple fix that solved the problem for me (in Python 2.4). This would probably work in future versions too.

Assume this is your basic XMLRPC Server set up in Python:

import SimpleXMLRPCServer

class FuncGroup:
    """Just hold the funcs to be used in web service"""
    def hi(self,val):
        return "hi there"

#create server and start it
server_address = ('localhost',8000) # (address, port)
server = SimpleXMLRPCServer.SimpleXMLRPCServer(server_address)
server.register_instance(FuncGroup()) #reg. functions/class instance
server.serve_forever()


Here is the fixed version. I added in an override for the trouble function:

import SimpleXMLRPCServer

#new code
import BaseHTTPServer
def not_insane_address_string(self):
    host, port = self.client_address[:2]
    return '%s (no getfqdn)' % host #used to call: socket.getfqdn(host)
BaseHTTPServer.BaseHTTPRequestHandler.address_string = \
    not_insane_address_string
#end new code

class FuncGroup:
    """Just hold the funcs to be used in web service"""
    def hi(self,val):
        return "hi there"

#create server and start it
server_address = ('localhost',8000) # (address, port)
server = SimpleXMLRPCServer.SimpleXMLRPCServer(server_address)
server.register_instance(FuncGroup()) #reg. functions/class instance
server.serve_forever()

One Response to “XMLRPC Server Slow in Python – How to Fix”

  1. Nice! Thanks!