SimpleXMLRPCServer runnning as a Windows Service using win32serviceutil

JDF jdferrell3 at gmail.com
Wed Apr 26 12:54:12 EDT 2006


I am trying to create a Windows service using SimpleXMLRPCServer and
win32serviceutil.  The service itself seems to be working properly
(starts, stops, etc) and I can connect using an XMLRPC client from the
localhost.  However when I connect from a remote client, I either get a
socket error or an xmlrpclib.ProtocolError error.  If I use
serve_forever() rather than handle_request(), the remote clients can
connect but it breaks the Windows service functionality (can't stop the
service).  I have tried the same code without the service and that
works, both locally and remotely.  It would seem that the problem is
related to the way the service handles remote connections, but I cannot
figure out what the problem is.

I have searched around, but can't find any example code.  Hopefully
someone can point me in the right direction.

thanks,
John

## XML-RPC Service
import win32serviceutil
import win32service
import win32event
import win32evtlogutil
import win32file
import servicemanager
import SimpleXMLRPCServer

class OBJECT:
    def hello(self):
        return "Hello World"

class XMLRPCSERVICE(win32serviceutil.ServiceFramework):
    _svc_name_ = "XMLRPCSERVICE"
    _svc_display_name_ = "XMLRPCSERVICE"
    _svc_description_ = "XMLRPCSERVICE"

    def __init__(self, args):
        win32evtlogutil.AddSourceToRegistry(self._svc_display_name_,
sys.executable, "Application")
        win32serviceutil.ServiceFramework.__init__(self, args)

        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
        self.hSockEvent = win32event.CreateEvent(None, 0, 0, None)

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)

    def SvcDoRun(self):
        ## Write a started event
        servicemanager.LogMsg(
            servicemanager.EVENTLOG_INFORMATION_TYPE,
            servicemanager.PYS_SERVICE_STARTED,
            (self._svc_name_, ' (%s)' % self._svc_name_))

        server = SimpleXMLRPCServer.SimpleXMLRPCServer(("", 8080))
        object = OBJECT()
        server.register_instance(object)

        while 1:
            win32file.WSAEventSelect(server,
self.hSockEvent,win32file.FD_ACCEPT)
            rc =
win32event.WaitForMultipleObjects((self.hWaitStop,self.hSockEvent), 0,
win32event.INFINITE)
            if rc == win32event.WAIT_OBJECT_0:
                break
            else:
                win32file.WSAEventSelect(server,self.hSockEvent, 0)
                server.handle_request()
                #server.serve_forever()  ## Works, but breaks the
Windows service functionality

        ## Write a stopped event
        win32evtlogutil.ReportEvent(self._svc_name_,

servicemanager.PYS_SERVICE_STOPPED,0,

servicemanager.EVENTLOG_INFORMATION_TYPE,
                                    (self._svc_name_,""))

if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(XMLRPCSERVICE)

----------------------------------------------------------------
##XML-RPC Server without service, this works using handle_request()
import SimpleXMLRPCServer

#The server object
class OBJECT:
    def hello(self):
        return "Hello World"

object = OBJECT()
server = SimpleXMLRPCServer.SimpleXMLRPCServer(("", 8080))
server.register_instance(object)

#Go into the main listener loop
print "Listening on port 8080"
while 1:
    server.handle_request()

----------------------------------------------------------------
## XML-RPC Client
import xmlrpclib

server = xmlrpclib.ServerProxy("http://remoteserver:8080")
print server.hello()




More information about the Python-list mailing list