Search code examples
pythonxml-rpc

Using Python xmlrpc between two RaspberryPis gives connection refused error


I'm trying to set up one RaspberryPi as server and another as client using Python3 xmlrpc. There are plenty of examples online, mostly along the same lines. The relevant server code snippet is:

from xmlrpc.server import SimpleXMLRPCServer
server = SimpleXMLRPCServer(("localhost", 6789))
server.serve_forever()

The client codes is:

import xmlrpc.client
proxy = xmlrpc.client.ServerProxy("http://localhost:6789")

This works fine for me, running in two separate terminal windows on one Pi. However, running the client code on the second Pi returns a connection refused error.

import xmlrpc.client
proxy = xmlrpc.client.ServerProxy("http://192.168.1.19:6789")

I can successfully ping the server from the client.

I suspect I might have misunderstood how this works. All the examples online are running client and server on the same machine and make no reference to anything but 'localhost'.

What am I missing?


Solution

  • OK, I found the answer. For anyone arriving here with the same problem this is what I've learned.

    The 'localhost' in the server code means only listen for connections from this machine. Changing this to '0.0.0.0' means listen on "all IPv4 addresses on the local machine" - Wikipedia. So I made the change and it worked.

    But I also did some tests. As shown, my server Pi is at 192.168.1.19, but changing the client code call to 192.168.1.100 connected just the same. Perhaps '0.0.0.0' means just all IPv4 addresses, but I don't want the server code to spark up in response to every network call. Changing the server code from '0.0.0.0' to its actual address of '192.168.1.19' made it ignore all other addresses. In my world I'm unlikely to have a machine with more than one IP address.