Skip to content

Add Support for Timeout Parameter #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions wordpress_xmlrpc/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,31 @@

from wordpress_xmlrpc.compat import xmlrpc_client, dict_type
from wordpress_xmlrpc.exceptions import ServerConnectionError, UnsupportedXmlrpcMethodError, InvalidCredentialsError, XmlrpcDisabledError
from wordpress_xmlrpc.timeout_transport import TimeoutTransport


class Client(object):

"""
Connection to a WordPress XML-RPC API endpoint.

To execute XML-RPC methods, pass an instance of an
`XmlrpcMethod`-derived class to `Client`'s `call` method.
"""

def __init__(self, url, username, password, blog_id=0):
def __init__(self, url, username, password, blog_id=0, timeout=30):
self.url = url
self.username = username
self.password = password
self.blog_id = blog_id
self.timeout = timeout

try:
self.server = xmlrpc_client.ServerProxy(url, allow_none=True)
transport = TimeoutTransport(timeout=self.timeout)
self.server = xmlrpc_client.ServerProxy(
url,
allow_none=True,
transport=transport)
self.supported_methods = self.server.mt.supportedMethods()
except xmlrpc_client.ProtocolError:
e = sys.exc_info()[1]
Expand All @@ -47,6 +54,7 @@ def call(self, method):


class XmlrpcMethod(object):

"""
Base class for XML-RPC methods.

Expand All @@ -68,10 +76,12 @@ def __init__(self, *args, **kwargs):
if self.optional_args:
max_num_args = len(self.method_args) + len(self.optional_args)
if not (len(self.method_args) <= len(args) <= max_num_args):
raise ValueError("Invalid number of parameters to %s" % self.method_name)
raise ValueError("Invalid number of parameters to %s" %
self.method_name)
else:
if len(args) != len(self.method_args):
raise ValueError("Invalid number of parameters to %s" % self.method_name)
raise ValueError("Invalid number of parameters to %s" %
self.method_name)

for i, arg_name in enumerate(self.method_args):
setattr(self, arg_name, args[i])
Expand Down Expand Up @@ -132,13 +142,15 @@ def process_result(self, raw_result):


class AnonymousMethod(XmlrpcMethod):

"""
An XML-RPC method for which no authentication is required.
"""
pass


class AuthenticatedMethod(XmlrpcMethod):

"""
An XML-RPC method for which user authentication is required.

Expand Down
33 changes: 33 additions & 0 deletions wordpress_xmlrpc/timeout_transport.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import httplib
import socket
import xmlrpclib


class TimeoutHTTPConnection(httplib.HTTPConnection):

def connect(self):
httplib.HTTPConnection.connect(self)
self.sock.settimeout(self.timeout)


class TimeoutHTTP(httplib.HTTP):
_connection_class = TimeoutHTTPConnection

def set_timeout(self, timeout):
self._conn.timeout = timeout


class TimeoutTransport(xmlrpclib.Transport):

def __init__(self, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
*args, **kwargs):
xmlrpclib.Transport.__init__(self, *args, **kwargs)
self.timeout = timeout

def make_connection(self, host):
if self._connection and host == self._connection[0]:
return self._connection[1]

chost, self._extra_headers, x509 = self.get_host_info(host)
self._connection = host, httplib.HTTPConnection(chost)
return self._connection[1]