Skip to content

Proxy support added to Client class. #41

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 21 additions & 4 deletions wordpress_xmlrpc/base.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import collections
import sys

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

class ProxiedTransport(xmlrpc_client.Transport):
def set_proxy(self, proxy):
self.proxy = proxy
def make_connection(self, host):
self.realhost = host
h = httplib.HTTPConnection(self.proxy)
return h
def send_request(self, connection, handler, request_body):
connection.putrequest("POST", 'http://%s%s' % (self.realhost, handler))
def send_host(self, connection, host):
connection.putheader('Host', self.realhost)


class Client(object):
"""
Expand All @@ -13,15 +25,20 @@ class Client(object):
`XmlrpcMethod`-derived class to `Client`'s `call` method.
"""

def __init__(self, url, username, password, blog_id=0):
def __init__(self, url, username, password, proxy='', blog_id=0):
self.url = url
self.username = username
self.password = password
self.blog_id = blog_id

try:
self.server = xmlrpc_client.ServerProxy(url, allow_none=True)
self.supported_methods = self.server.mt.supportedMethods()
if proxy != '':
p = ProxiedTransport()
p.set_proxy(proxy)
self.server = xmlrpc_client.ServerProxy(url, allow_none=True, transport=p)
else:
self.server = xmlrpc_client.ServerProxy(url, allow_none=True)
self.supported_methods = self.server.mt.supportedMethods()
except xmlrpc_client.ProtocolError:
e = sys.exc_info()[1]
raise ServerConnectionError(repr(e))
Expand Down