将 Python xmlrpclib 与 unix 域套接字一起使用? [英] Use Python xmlrpclib with unix domain sockets?

查看:18
本文介绍了将 Python xmlrpclib 与 unix 域套接字一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试与 supervisord 进行交互,我想通过 Unix 套接字(它是一个共享托管环境)与它交谈.

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

/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?

推荐答案

xmlrpclib 要求传递的 url 以 httphttps 开头.解决这个问题的方法是定义一个忽略该 url 的自定义传输.下面是一些使用来自主管的传输的代码:

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)

这篇关于将 Python xmlrpclib 与 unix 域套接字一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆