473,665 Members | 2,827 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Windows service using the socket module

JDF
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()

Nov 6 '05 #1
1 2222
On 6 Nov 2005 12:26:07 -0800, "JDF" <jd********@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.

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
Given the above comment, I'm surprised anything works... Where is
the overlapped I/O event queued?
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()
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.
-- =============== =============== =============== =============== == <
wl*****@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
=============== =============== =============== =============== == <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.ne tcom.com/> <

Nov 6 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
565
by: Martijn Remmen | last post by:
I have developed a service which exposes a COM object. This service is running perfect on Windows 2000 Server and Windows 2000 Professional under the SYSTEM account. When the service is installed on Windows 2003 Server, the application works well, and the COM object can be called just as it's meant to be. The service is however running under the IWAM account (I know strange account to run a service). However, after a while (from...
2
3274
by: raghavendra | last post by:
Hi, How to run automatically windows service by using setup deployment insatllation script using visual studio 2003.? What i did is :-- 1. created a windows service & tested the same. 2. then i used the windowsservice.exe in setup deployment project.
2
2932
by: Neslihan ERDEM | last post by:
Every body Hi first of all I say Why do I need Windows Service / Every Day I create XML file . I writed a XML web service And .I join this servis Windows service. I create Windows Service that I call method XML Web Service . My Problem I generate Windows Service but I want this service always run . But I dont make it
0
1270
by: Michel Hardy | last post by:
Hi, I have a windows application that broadcast a message using a socket in udp. It receives the broadcast and show the sender's ip address. And it works fine. I use the same code in a windows service that logs on a administrator not local system and instead of receiving the sender's ip i get the broadcast ip 255.255.255.255. Any body knows why? Here is the code:
3
26204
by: Sean | last post by:
Greetings, Is there a sample code that shows how to restart a windows service using VB.Net? Sean
0
1994
by: JDF | last post by:
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...
6
2893
by: Ravi Shankar | last post by:
Hello, I have written a service using VB.Net which is basically a asynch socket listener. I have been able to install it successfully. Using the Service Control Manager I am able to start & stop the service successfully. Once started, the service is responding to requests. The service runs under a user a/c (the user is part of the local administrators group). I have set the ServiceDependsOn to TCP/IP Protocol Driver, Event Log, Remote...
2
3500
by: Avi | last post by:
Hi all, How can I profile my windows service using performance Explorer? Thanks, Avi
0
1626
vdraceil
by: vdraceil | last post by:
Can anyone explain how to make a windows service using vb6? I'm creating an application that will be running all the time.so instead of a exe, i prefer services. I know it is possible to make services using vb,but i dont know how..somebody pls help
0
8438
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8348
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8863
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7376
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4356
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1761
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.