使用Python的xmlrpclib与UNIX域套接字?(Use Python xmlrpclib

2019-06-26 15:12发布

我试图与互动supervisord ,我想在Unix套接字(它是一个共享的主机环境中)与它交谈。

什么到目前为止,我已经试过是:

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肯定存在。 形式的URI的“UNIX:///path/to/supervisor.sock/RPC2”被使用supervisord ,这就是我的想法。 该文档不讨论Unix套接字: http://docs.python.org/library/xmlrpclib.html 。

这可能吗? 我应该使用不同的图书馆吗?

Answer 1:

xmlrpclib要求URL传递开始与httphttps 。 解决这个问题的方法是定义忽略了URL定制的运输。 下面是使用由主管运输的一些代码:

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()

如果这是没有用的,这里发现的代码的更新版本在这里 :

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)


Answer 2:

下面是使用的xmlrpclib去跟上司更新的例子:

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())

正如已经提到,我们必须以http指定一个虚拟地址://或https://,然后指定一个自定义传输处理域套接字



Answer 3:

我的版本python3,从以上的版本准备。

class UnixStreamHTTPConnection(http.client.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("/some/supervisor/socket/file/path"))

 print(proxy.supervisor.getState())


Answer 4:

混合上述问题的答案,这里对我来说是什么在起作用?

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()


文章来源: Use Python xmlrpclib with unix domain sockets?