Use Python xmlrpclib with unix domain sockets?

Viewed 8403

I'm trying to interact with supervisord, and I'd like to talk with it over a unix socket (it's a shared hosting environment).

What I've tried so far is:

import xmlrpclib
server = xmlrpclib.ServerProxy('unix:///path/to/supervisor.sock/RPC2')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/xmlrpclib.py", line 1549, in __init__
    raise IOError, "unsupported XML-RPC protocol"
IOError: unsupported XML-RPC protocol

/path/to/supervisor.sock definitely exists. URIs of the form 'unix:///path/to/supervisor.sock/RPC2' are used by supervisord, which is where I got the idea. The docs don't discuss unix sockets: http://docs.python.org/library/xmlrpclib.html.

Is this possible? Should I use a different library?

4 Answers

My version for python3, prepared from versions above.

from http.client import HTTPConnection
import socket
from xmlrpc import client

class UnixStreamHTTPConnection(HTTPConnection):
    def connect(self):
        self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        self.sock.connect(self.host)

class UnixStreamTransport(client.Transport, object):
    def __init__(self, socket_path):
        self.socket_path = socket_path
        super(UnixStreamTransport, self).__init__()

    def make_connection(self, host):
        return UnixStreamHTTPConnection(self.socket_path)

proxy = client.ServerProxy('http://localhost', transport=UnixStreamTransport("/var/run/supervisor.sock"))

print(proxy.supervisor.getState())
Related