TCP server as a Windows service using Python?
johnfabel at btinternet.com
Sat Jan 31 22:57:47 CET 2004
I don't know if this will be of use to you. Here's a snip of code, that
I used to run a web server as a service. It uses my custom module based
on SimpleHTTPServer, but you get the idea.
def SvcDoRun( self ):
threadEvent = threading.Event()
webUI = httpWebService( threadEvent )
class httpWebService( threading.Thread ):
def __init__( self, eventNotifyObj ):
threading.Thread.__init__( self )
self.notifyEvent = eventNotifyObj
def run ( self ):
serverPort = 10080
httpServer = SimpleHTTPServer2.HTTPServer( ('', serverPort),
httpServerWait = httpServer.fileno()
httpReady = select.select( [httpServerWait], , , 1)
if httpServerWait in httpReady:
David Mitchell wrote:
>I'm trying to create a TCP server using Python, and I want it to run under
>Windows as a service.
>Now, I'm fine with building the TCP server using Python - done it lots of
>times, and I know there's lots of sample code out there I can grab if I
>ever need to.
>Equally, I think I've got the simpler concepts about creating Windows
>services using Python clear. I've got the Python Win32 book, worked
>through the example code in there and haven't had any real problems with
>Where I'm having trouble is with the overlapped I/O call. When I create a
>TCP server, I need to have it sit there waiting for a client
>connection. With a Windows service, it has to sit there waiting for
>administrative messages such as e.g. a "stop service" message. The
>example code in the Python Win32 book uses named pipes to do IO -
>I'm constrained to using TCP sockets.
>I can't see how to let the service accept *either* an incoming TCP client
>connection *or* an e.g. "stop service" message. If someone could point me
>to some sample code, I'd greatly appreciate it - Google hasn't been
>Thanks in advance
More information about the Python-list