Python’s SimpleXMLRPCServer – How to Serve from a Different Machine

It turns out that following the example from Python’s SimpleXMLRPCServer documentation of serving from localhost only works for well, localhost:

from SimpleXMLRPCServer import SimpleXMLRPCServer
# Create server
server = SimpleXMLRPCServer(("localhost", 8000))
#snip ...(register functions here, see example)
server.register_instance(MyFuncs())
# Run the server's main loop
server.serve_forever()

To make your server available to other computers on your network you should do something like this:

import socket
from SimpleXMLRPCServer import SimpleXMLRPCServer
# Create server
server = SimpleXMLRPCServer((socket.gethostbyname(socket.gethostname()), 8000))
#snip ...(register functions here, see example)
server.register_instance(MyFuncs())
server.serve_forever()

Open question, will this work for serving to the internet at large, or do I need to do something differently? I’m actually not sure why localhost doesn’t work across the LAN. Does anyone know?

I found this answer by a google code search for SimpleXMLRPCServer.

3 Responses to “Python’s SimpleXMLRPCServer – How to Serve from a Different Machine”

  1. Jeff McNeil says:

    The first argument to SimpleXMLRPCServer() is a tuple containing the address that the server will bind to. When passing in (localhost, 8080), you’re telling the server that it should bind to 127.0.0.1:8080. This is your local loopback device – you can only access this address from the local system.

    Your updated tuple (socket.gethostname(), 8080) passes your *real* host name in. Your local host name is resolved to a ‘real world’ IP address, and SimpleXMLRPCServer binds to that.

    It should let you serve requests off of the Internet. I’d personally front my installation with Apache, or perhaps use something like Zope et al. to run an internet facing XMLRPC installation.

    Remember that you’re telling Python to bind to the address/port pair in that first tuple argument. It might be a good idea to know which IP you want to serve requests from and pass that directly, perhaps via a configuration file option or something, depending on the scale of your project.

  2. Jeff, thanks for that detailed explanation. That clears everything up.

  3. [...] Python’s SimpleXMLRPCServer – How to Serve from a Different Machine [...]