可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
xmlrpclib
requires that the url passed start with http
or https
. The way around this is to define a custom transport which ignores that url. Here's some code using the transport from supervisor:
import supervisor.xmlrpc
import xmlrpclib
proxy = xmlrpclib.ServerProxy('http://127.0.0.1',
transport=supervisor.xmlrpc.SupervisorTransport(
None, None, serverurl='unix://'+socketpath))
proxy.supervisor.getState()
In case that's not useful, here's an updated version of the code found here:
class UnixStreamHTTPConnection(httplib.HTTPConnection, object):
def __init__(self, *args, **kwargs):
self.socketpath = kwargs.pop('socketpath')
super(UnixStreamHTTPConnection, self).__init__(*args, **kwargs)
def connect(self):
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.sock.connect_ex(self.socketpath)
class UnixStreamTransport(xmlrpclib.Transport, object):
def __init__(self, *args, **kwargs):
self.socketpath = kwargs.pop('socketpath')
super(UnixStreamTransport, self).__init__(*args, **kwargs)
回答2:
Here's an updated example for using xmlrpclib to talk to supervisor:
import httplib
import socket
import xmlrpclib
class UnixStreamHTTPConnection(httplib.HTTPConnection):
def connect(self):
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.sock.connect(self.host)
class UnixStreamTransport(xmlrpclib.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)
server = xmlrpclib.Server('http://arg_unused', transport=UnixStreamTransport("/var/run/supervisor.sock"))
print(server.supervisor.getState())
As has already been mentioned we have to specify a dummy url with http:// or https:// and then specify a custom transport to handle the domain socket
回答3:
Mixing the answers above, here's what works for me...
import httplib
import socket
import xmlrpclib
class UnixStreamHTTPConnection(httplib.HTTPConnection, object):
def __init__(self, *args, **kwargs):
self.socketpath = kwargs.pop('socketpath')
super(UnixStreamHTTPConnection, self).__init__(*args, **kwargs)
def connect(self):
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.sock.connect(self.socketpath)
class UnixStreamTransport(xmlrpclib.Transport, object):
def __init__(self, *args, **kwargs):
self.socketpath = kwargs.pop('socketpath')
super(UnixStreamTransport, self).__init__(*args, **kwargs)
def make_connection(self, host):
return UnixStreamHTTPConnection(host, socketpath=self.socketpath)
server = xmlrpclib.ServerProxy('http://arg_unused', transport=UnixStreamTransport(socketpath="path/to/supervisor.sock"))
print server.supervisor.getState()
回答4:
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())