Safe Local XMLRPC

Michael Urman murman at gmail.com
Sat Mar 12 14:07:52 EST 2005


Thanks for your time everyone; I got it XMLRPC working over unix
domain stream sockets. In case people are interested, here's the
pieces I put together. I'm sure they throw away a little flexibility,
but they work for my purpose. Any pointers to make the code more
robust, or do less total overriding of should-be-arguments
appreciated.

-m

from SocketServer import UnixStreamServer
from SimpleXMLRPCServer import SimpleXMLRPCDispatcher,
SimpleXMLRPCRequestHandler
from xmlrpclib import ServerProxy, Fault, Transport
from socket import socket, AF_UNIX, SOCK_STREAM

# Server side is pretty easy - almost a direct copy of SimpleXMLRPCServer
SOCKPATH = 'testsock'
class UnixStreamXMLRPCServer(UnixStreamServer, SimpleXMLRCPDispatcher):
    def__init__(self, addr=SOCKPATH, requestHandler=SimpleXMLRPCRequestHandler):
        self.logRequests = 0 # critical, as logging fails with UnixStreamServer
        SimpleXMLRPCDispatcher.__init__(self)
        UnixStreamserver.__Init__(self, addr, requestHandler)

# Client is a lot more complicated and feels fragile
from httplib import HTTP, HTTPConnection
class UnixStreamHTTPConnection(HTTPConnection):
    def connect(self):
        self.sock = socket(AF_UNIX, SOCK_STREAM)
        self.sock.connect(SOCKPATH)

class UnixStreamHTTP(HTTP):
    _connection_class = UnixStreamHTTPConnection

class UnixStreamTransport(Transport):
    def make_connection(self, host):
        return UnixStreamHTTP(SOCKPATH) # overridden, but prevents IndexError

proxy = ServerProxy('http://' + SOCKPATH, transport=UnixStreamTransport())
# proxy now works just like any xmlrpclib.ServerProxy



More information about the Python-list mailing list