Windows service using the socket module

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

    #1

    Windows service using the socket module

    I am fairly new to python and seem to have gotten ahead of myself. I
    am trying to write a Windows service that will return the current
    number of Citrix sessions to a client. This service will run on my
    Citrix servers for usage tracking purposes.

    The service itself seems to function properly, it starts, stops, logs
    to the event viewer, etc. The problem seems to be with the portion
    where it either waits to be stopped or waits for remote connection - it
    does not get to the 'wait for remote connection' part.

    If I remove the 'wait for stop request' portion the service will return
    the session count but of course I can't stop the service nicely.

    Does anyone have some example code I could take a look at or some
    advice to get me back on track?

    thanks in advance,
    John

    s = socket.socket()
    hostIP = socket.gethostb yaddr(socket.ge thostname())[2][0]
    if debug == 'true': servicemanager. LogInfoMsg('Hos t IP: ' +
    str(hostIP))
    s.bind((hostIP, port))

    while 1:
    # Wait for either a connection, or a service stop request.
    timeout = win32event.INFI NITE
    waitHandles = self.hWaitStop, self.overlapped .hEvent
    rc = win32event.Wait ForMultipleObje cts(waitHandles , 0, timeout)
    if rc == win32event.WAIT _OBJECT_0:
    # Stop event
    break
    else:
    s.listen(5)
    while True:
    sessions = 0
    qwinstaOut = os.popen(qwinst aCmd).readlines ()
    for line in qwinstaOut:
    ## Look for active ICA sessions
    if (((re.search("I CA", line, re.IGNORECASE)) :
    sessions = sessions + 1
    c, addr = s.accept()
    ## Send session count
    c.send(str(sess ions))
    c.close()

  • Dennis Lee Bieber

    #2
    Re: Windows service using the socket module

    On 6 Nov 2005 12:26:07 -0800, "JDF" <jdferrell3@gma il.com> declaimed the
    following in comp.lang.pytho n:

    You left off the important stuff... Like the class definition, since
    you reference "self." -- there is nothing here to tell where
    self.hWaitStop or self.overlapped .hEvent are created/defined.[color=blue]
    >
    > s = socket.socket()
    > hostIP = socket.gethostb yaddr(socket.ge thostname())[2][0]
    > if debug == 'true': servicemanager. LogInfoMsg('Hos t IP: ' +
    > str(hostIP))
    > s.bind((hostIP, port))
    >
    > while 1:
    > # Wait for either a connection, or a service stop request.
    > timeout = win32event.INFI NITE
    > waitHandles = self.hWaitStop, self.overlapped .hEvent[/color]

    Given the above comment, I'm surprised anything works... Where is
    the overlapped I/O event queued?
    [color=blue]
    > rc = win32event.Wait ForMultipleObje cts(waitHandles , 0, timeout)
    > if rc == win32event.WAIT _OBJECT_0:
    > # Stop event
    > break
    > else:
    > s.listen(5)
    > while True:
    > sessions = 0
    > qwinstaOut = os.popen(qwinst aCmd).readlines ()
    > for line in qwinstaOut:
    > ## Look for active ICA sessions
    > if (((re.search("I CA", line, re.IGNORECASE)) :
    > sessions = sessions + 1
    > c, addr = s.accept()
    > ## Send session count
    > c.send(str(sess ions))
    > c.close()[/color]

    I'd be wary of mixing basic socket operations with Windows I/O event
    scheme... But maybe that is handled in the missing code.

    And how would you expect to get out of that inner loop? There is no
    "break" so if the code ever gets to the s.listen() call (and past it) it
    will never get back to the top loop with the event wait. Someone else
    will have to clarify -- but isn't s.accept() a blocking call itself?

    If the I/O doesn't play nice together, it may be better to put the
    socket handling into a thread with a time-out select() call, so the
    thread can periodically test for the presense of a shutdown flag. The
    main program would do the wait for the shutdown, set the flag so the
    thread can see it -- the thread would then close up its stuff and
    return(exit); the main would be waiting on thread.join() so it knows
    when things are clean enough to exit itself.
    --[color=blue]
    > =============== =============== =============== =============== == <
    > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
    > wulfraed@dm.net | Bestiaria Support Staff <
    > =============== =============== =============== =============== == <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color]

    Comment

    Working...