Timeout for xmlrpclib client requests

I am using Python xmlrpclib to query xml-rpc service requests.

Is there a way to set the client timeout so my requests do not freeze forever when the server is unavailable?

I know that all over the world you can set the socket timeout with socket.setdefaulttimeout() , but this is not desirable.

+4
source share
4 answers

A clean approach is to define and use custom transport, for example:! this will only work 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) 

Here is an example of defining and using a custom transport in documents , although it uses it for another purpose (access through a proxy, instead of setting timeouts), this code is mostly inspired by this example.

+12
source

doh to make this work in python2.6 + to 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 
+8
source

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 like nothing more than compatibility classes for older versions of Python.

+5
source

An alternative implementation that will be compatible with python 2.7 will be as follows (with a comment containing what you want if 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 will allow the main implementation of 2.7 to support its supporting HTTP / 1.1 keep-alive functionality.

It should be noted that if you are trying to use XML-RPC through an https connection / address, replace the xmlrpc.SafeTransport links with xmlrpc.Transport instead and if you are using the 2.6 implementation, use httplib.HTTPSConnection .

+3
source

Source: https://habr.com/ru/post/1303814/


All Articles