win32 service and sockets

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom Brown

    #1

    win32 service and sockets

    Hi,

    I created a win32 service for XPPro called N4010ATestServi ce.py (see below).
    The service runs as a particular user with administrative rights. It starts a
    thread that creates a simple socket server (N4010ASocketSe rver.py -- also
    below) that just waits for 20 character string. When I run the socket server
    by itself the test client can connect to the server and send a 20 character
    string just fine. When I run the service, the server will bind to the port
    but the client cannot connect. I verified the server was listening on the
    given port using netstat -an. The client eventually times out. Why isn't the
    server accepting connections when run in a service?

    Thanks,
    Tom

    N4010ATestServi ce.py:
    ---------------------
    import win32serviceuti l
    import win32service
    import win32event
    import N4010ASocketSer ver
    from thread import start_new_threa d

    class N4010ATestServi ce(win32service util.ServiceFra mework):
    _svc_name_ = "N4010ATestServ ice"
    _svc_display_na me_ = "N4010A Test Service"
    def __init__(self, args):
    win32serviceuti l.ServiceFramew ork.__init__(se lf, args)

    def SvcDoRun(self):
    start_new_threa d(N4010ASocketS erver.main, ())
    N4010ASocketSer ver.waitfor()

    def SvcStop(self):
    # Before we do anything, tell the SCM we are starting the stop process.
    self.ReportServ iceStatus(win32 service.SERVICE _STOP_PENDING)
    N4010ASocketSer ver.stop()

    if __name__ == '__main__':
    win32serviceuti l.HandleCommand Line(N4010ATest Service)


    N4010ASocketSer ver.py:
    ----------------------
    from socket import socket
    from select import select
    from time import sleep
    import win32evtlogutil

    applicationName = 'N4010ASocketSe rver'
    messageDll = 'C:\Python24\Li b\site-packages\win32\ pythonservice.e xe'
    running = True
    stopped = False

    def registerWithEve ntViewer():
    win32evtlogutil .AddSourceToReg istry(applicati onName, messageDll)

    def stop():
    import servicemanager
    servicemanager. LogInfoMsg('sto pping')
    global running
    running = False

    def waitfor():
    while not stopped:
    sleep(0.5)

    def main():
    import servicemanager
    registerWithEve ntViewer()
    servicemanager. LogInfoMsg('cre ating socket')
    sock = socket()
    servicemanager. LogInfoMsg('bin ding to port 48777')
    sock.bind(('0.0 .0.0', 48777))
    servicemanager. LogInfoMsg('lis tening')
    sock.listen(5)
    while running:
    servicemanager. LogInfoMsg('Wai ting for connection.')
    readyRead, readyWrite, inerror = select([sock], [], [], 0)
    while (not readyRead) and running:
    sleep(0.5)
    readyRead, readyWrite, inerror = select([sock], [], [], 0)
    if running:
    conn, address = sock.accept()
    msg = conn.recv(20)
    servicemanager. LogInfoMsg('Rec vd msg: %s' % msg)
    conn.close()
    global stopped
    stopped = True
Working...