xmlrpclib 客户端请求超时

发布于 2024-08-25 12:03:00 字数 168 浏览 4 评论 0原文

我正在使用 Python 的 xmlrpclib 向 xml-rpc 服务发出请求。

有没有办法设置客户端超时,这样当服务器不可用时我的请求就不会永远挂起?

我知道我可以使用 socket.setdefaulttimeout() 全局设置套接字超时,但这并不可取。

I am using Python's xmlrpclib to make requests to an xml-rpc service.

Is there a way to set a client timeout, so my requests don't hang forever when the server is not available?

I know I can globally set a socket timeout with socket.setdefaulttimeout(), but that is not preferable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风月客 2024-09-01 12:03:00

干净的方法是定义和使用自定义传输,例如:
!这仅适用于 python2.7 !

import xmlrpclib, httplib

class TimeoutTransport(xmlrpclib.Transport):
    timeout = 10.0
    def set_timeout(self, timeout):
        self.timeout = timeout
    def make_connection(self, host):
        h = httplib.HTTPConnection(host, timeout=self.timeout)
        return h

t = TimeoutTransport()
t.set_timeout(20.0)
server = xmlrpclib.Server('http://time.xmlrpc.com/RPC2', transport=t)

中有一个定义和使用自定义传输的示例文档,尽管它将其用于不同的目的(通过代理访问,而不是设置超时),但此代码基本上受到该示例的启发。

The clean approach is to define and use a custom transport, e.g.:
! this will work only for python2.7 !

import xmlrpclib, httplib

class TimeoutTransport(xmlrpclib.Transport):
    timeout = 10.0
    def set_timeout(self, timeout):
        self.timeout = timeout
    def make_connection(self, host):
        h = httplib.HTTPConnection(host, timeout=self.timeout)
        return h

t = TimeoutTransport()
t.set_timeout(20.0)
server = xmlrpclib.Server('http://time.xmlrpc.com/RPC2', transport=t)

There's an example of defining and using a custom transport in the docs, though it's using it for a different purpose (access via a proxy, rather than setting timeouts), this code is basically inspired by that example.

猫弦 2024-09-01 12:03:00

哦,要使其在 python2.6+ 中工作,请执行以下操作:

class HTTP_with_timeout(httplib.HTTP):
    def __init__(self, host='', port=None, strict=None, timeout=5.0):
        if port == 0: port = None
        self._setup(self._connection_class(host, port, strict, timeout=timeout))

    def getresponse(self, *args, **kw):
        return self._conn.getresponse(*args, **kw)

class TimeoutTransport(xmlrpclib.Transport):
    timeout = 10.0
    def set_timeout(self, timeout):
        self.timeout = timeout
    def make_connection(self, host):
        h = HTTP_with_timeout(host, timeout=self.timeout)
        return h

doh, to make this work in python2.6+ do this:

class HTTP_with_timeout(httplib.HTTP):
    def __init__(self, host='', port=None, strict=None, timeout=5.0):
        if port == 0: port = None
        self._setup(self._connection_class(host, port, strict, timeout=timeout))

    def getresponse(self, *args, **kw):
        return self._conn.getresponse(*args, **kw)

class TimeoutTransport(xmlrpclib.Transport):
    timeout = 10.0
    def set_timeout(self, timeout):
        self.timeout = timeout
    def make_connection(self, host):
        h = HTTP_with_timeout(host, timeout=self.timeout)
        return h
罪歌 2024-09-01 12:03:00

为什么不:

class TimeoutTransport(xmlrpclib.Transport):

def setTimeout(self, timeout):
    self._timeout = timeout

def make_connection(self, host):
    return httplib.HTTPConnection(host, timeout=self._timeout)

毕竟,HTTPHTTPS 似乎只不过是旧版 Python 版本的兼容性类。

Why not:

class TimeoutTransport(xmlrpclib.Transport):

def setTimeout(self, timeout):
    self._timeout = timeout

def make_connection(self, host):
    return httplib.HTTPConnection(host, timeout=self._timeout)

?

After all, HTTP and HTTPS seem to be no more than compatibility classes for older Python versions.

じ违心 2024-09-01 12:03:00

与 python 2.7 兼容的替代实现如下(如果您使用 python 2.6,注释包含您想要的内容):

import socket
import xmlrpclib

class TimeoutTransport (xmlrpclib.Transport):
    """
    Custom XML-RPC transport class for HTTP connections, allowing a timeout in
    the base connection.
    """

    def __init__(self, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, use_datetime=0):
        xmlrpclib.Transport.__init__(self, use_datetime)
        self._timeout = timeout

    def make_connection(self, host):
        # If using python 2.6, since that implementation normally returns the 
        # HTTP compatibility class, which doesn't have a timeout feature.
        #import httplib
        #host, extra_headers, x509 = self.get_host_info(host)
        #return httplib.HTTPConnection(host, timeout=self._timeout)

        conn = xmlrpclib.Transport.make_connection(self, host)
        conn.timeout = self._timeout
        return conn

# Example use
t = TimeoutTransport(timeout=10)
server = xmlrpclib.ServerProxy('http://time.xmlrpc.com/RPC2', transport=t)

使用 super-method 将允许底层 2.7 实现保持其 HTTP/1.1 保留- 它定义的活动功能。

需要注意的是,如果您尝试通过 https 连接/地址使用 XML-RPC,请将 xmlrpc.SafeTransport 引用替换为 xmlrpc.Transport,并且,如果您使用 2.6 实现,请使用 httplib.HTTPSConnection

An alternative implementation that would be compatible with python 2.7 would be as follows (with a comment containing what you would want if you're using python 2.6):

import socket
import xmlrpclib

class TimeoutTransport (xmlrpclib.Transport):
    """
    Custom XML-RPC transport class for HTTP connections, allowing a timeout in
    the base connection.
    """

    def __init__(self, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, use_datetime=0):
        xmlrpclib.Transport.__init__(self, use_datetime)
        self._timeout = timeout

    def make_connection(self, host):
        # If using python 2.6, since that implementation normally returns the 
        # HTTP compatibility class, which doesn't have a timeout feature.
        #import httplib
        #host, extra_headers, x509 = self.get_host_info(host)
        #return httplib.HTTPConnection(host, timeout=self._timeout)

        conn = xmlrpclib.Transport.make_connection(self, host)
        conn.timeout = self._timeout
        return conn

# Example use
t = TimeoutTransport(timeout=10)
server = xmlrpclib.ServerProxy('http://time.xmlrpc.com/RPC2', transport=t)

Using the super-method would allow the underlying 2.7 implementation to maintain its HTTP/1.1 keep-alive functionality it defines.

A thing to note is that if you're trying to use XML-RPC over an https connection/address, replace xmlrpc.SafeTransport references with xmlrpc.Transport instead, and, if you're using the 2.6 implementation, use httplib.HTTPSConnection.

生生漫 2024-09-01 12:03:00

如果有人尝试在 Python 3+ 中执行此操作并使用上下文 kwarg(在我的例子中允许连接到自签名 SSL 证书),则以下代码适合我

class TimeoutTransport (xmlrpc.client.SafeTransport):
    def __init__(self, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, context=None, use_datetime=0):
        xmlrpc.client.Transport.__init__(self, use_datetime)
        self._timeout = timeout
        self.context = context

    def make_connection(self, host):
        conn = xmlrpc.client.SafeTransport.make_connection(self, host)
        conn.timeout = self._timeout
        return conn

然后调用:

 url = "https://localhost:8080/RPC2"
 t = TimeoutTransport(timeout=2, context=ssl._create_unverified_context())
 xml_conn = xmlrpc.client.ServerProxy(
        url,
        transport=t
    )

If anyone is trying to do this in Python 3+ and makes use of the context kwarg (In my case to allow connection to self-signed SSL certs), the following code works for me

class TimeoutTransport (xmlrpc.client.SafeTransport):
    def __init__(self, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, context=None, use_datetime=0):
        xmlrpc.client.Transport.__init__(self, use_datetime)
        self._timeout = timeout
        self.context = context

    def make_connection(self, host):
        conn = xmlrpc.client.SafeTransport.make_connection(self, host)
        conn.timeout = self._timeout
        return conn

And then call with:

 url = "https://localhost:8080/RPC2"
 t = TimeoutTransport(timeout=2, context=ssl._create_unverified_context())
 xml_conn = xmlrpc.client.ServerProxy(
        url,
        transport=t
    )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文