Windows service using the socket module

2005-11-06 Thread 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.gethostbyaddr(socket.gethostname())[2][0]
if debug == 'true': servicemanager.LogInfoMsg('Host IP: ' +
str(hostIP))
s.bind((hostIP, port))

while 1:
# Wait for either a connection, or a service stop request.
timeout = win32event.INFINITE
waitHandles = self.hWaitStop, self.overlapped.hEvent
rc = win32event.WaitForMultipleObjects(waitHandles, 0, timeout)
if rc == win32event.WAIT_OBJECT_0:
# Stop event
break
else:
s.listen(5)
 while True:
 sessions = 0
 qwinstaOut = os.popen(qwinstaCmd).readlines()
 for line in qwinstaOut:
 ## Look for active ICA sessions
 if (((re.search("ICA", line, re.IGNORECASE)):
 sessions = sessions + 1
 c, addr = s.accept()
 ## Send session count
 c.send(str(sessions))
 c.close()

-- 
http://mail.python.org/mailman/listinfo/python-list


SimpleXMLRPCServer runnning as a Windows Service using win32serviceutil

2006-04-26 Thread JDF
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()

-- 
http://mail.python.org/mailman/listinfo/python-list