Using SimpleXMLRPCServer in a Windows Service

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rudy Schockaert

    #1

    Using SimpleXMLRPCServer in a Windows Service

    After some Googling I found a post of someone who wanted to do exactly
    as what I want to do now.
    There is however a problem in his code that makes the service fails
    after the first connection. I slightly modified his code and now I can
    run the service longer before I run into trouble.
    I then tried making the SimpleXMLRPCSer ver multi-threaded, hoping the
    problem would disappear, but no avail.
    The code is as follows:
    The commented part in the while loop is from the original code.
    <CODE>


    ## XML-RPC Service
    import sys
    import win32serviceuti l
    import win32service
    import win32event
    import win32evtlogutil
    import win32file
    import servicemanager
    import SimpleXMLRPCSer ver
    import SocketServer
    import select

    class OBJECT:
    def hello(self, text):
    return "Hello World (%s)" % text

    class ThreadedSimpleX MLRPCServer(Soc ketServer.Threa dingMixIn,
    SimpleXMLRPCSer ver.SimpleXMLRP CServer): pass

    class XMLRPCSERVICE(w in32serviceutil .ServiceFramewo rk):
    _svc_name_ = "XMLRPCSERV ICE"
    _svc_display_na me_ = "XMLRPCSERV ICE"
    _svc_descriptio n_ = "XMLRPCSERV ICE"

    def __init__(self, args):
    win32evtlogutil .AddSourceToReg istry(self._svc _display_name_,
    sys.executable, "Applicatio n")
    win32serviceuti l.ServiceFramew ork.__init__(se lf, args)

    self.hWaitStop = win32event.Crea teEvent(None, 0, 0, None)
    self.hSockEvent = win32event.Crea teEvent(None, 0, 0, None)
    self.stop_reque sted = 0

    def SvcStop(self):
    self.ReportServ iceStatus(win32 service.SERVICE _STOP_PENDING)
    self.stop_reque sted = 1
    win32event.SetE vent(self.hWait Stop)

    def SvcDoRun(self):
    ## Write a started event
    servicemanager. LogMsg(
    servicemanager. EVENTLOG_INFORM ATION_TYPE,
    servicemanager. PYS_SERVICE_STA RTED,
    (self._svc_name _, ' (%s)' % self._svc_name_ ))

    server = ThreadedSimpleX MLRPCServer(("" , 8080))
    object = OBJECT()
    server.register _instance(objec t)
    self.socket = server.socket

    while 1:
    r, w, x = select.select([self.socket],[],[],10)
    if r == [self.socket]:
    server.handle_r equest()
    if self.stop_reque sted:
    self.socket.clo se()
    break


    #win32file.WSAE ventSelect(serv er,
    self.hSockEvent ,win32file.FD_A CCEPT)
    #rc =
    win32event.Wait ForMultipleObje cts((self.hWait Stop,self.hSock Event), 0,
    win32event.INFI NITE)
    #if rc == win32event.WAIT _OBJECT_0:
    # break
    #else:
    # server.handle_r equest()
    # win32file.WSAEv entSelect(serve r,self.hSockEve nt, 0)
    # #server.serve_f orever() ## Works, but breaks the

    ## Write a stopped event
    win32evtlogutil .ReportEvent(se lf._svc_name_,
    servicemanager. PYS_SERVICE_STO PPED,0,
    servicemanager. EVENTLOG_INFORM ATION_TYPE,
    (self._svc_name _,""))

    if __name__ == '__main__':
    win32serviceuti l.HandleCommand Line(XMLRPCSERV ICE)


    </CODE>

    I tested with the following:

    <CODE>

    import xmlrpclib
    import time

    server = xmlrpclib.Serve rProxy("http://localhost:8080" )
    for i in range(100):
    print server.hello("% d" % i)
    time.sleep(1)

    </CODE>

    The loop ends with the following error:

    <OUTPUT>
    Hello World (0)
    ....
    Hello World (44)
    Traceback (most recent call last):
    File "C:\Python24\Li b\site-packages\python win\pywin\frame work\scriptutil s.py",
    line 310, in RunScript
    exec codeObject in __main__.__dict __
    File "C:\DATA\TestSo ap.py", line 6, in ?
    print server.hello("% d" % i)
    File "C:\Python24\li b\xmlrpclib.py" , line 1096, in __call__
    return self.__send(sel f.__name, args)
    File "C:\Python24\li b\xmlrpclib.py" , line 1383, in __request
    verbose=self.__ verbose
    File "C:\Python24\li b\xmlrpclib.py" , line 1137, in request
    headers
    ProtocolError: <ProtocolErro r for localhost:8080/RPC2: -1 >
    </OUTPUT>

    Can someone help me in creating a windows service that allows me to
    handle XMLRPC request?

    Thanks in advance,

    Rudy Schockaert
Working...