TCP server as a Windows service using Python?

John Abel johnfabel at
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 )

        win32event.WaitForSingleObject( self.hWaitStop, 
win32event.INFINITE )


class httpWebService( threading.Thread ):

    def __init__( self, eventNotifyObj ):
        threading.Thread.__init__( self )
        self.notifyEvent = eventNotifyObj

    def run ( self ):
        serverPort = 10080
        SimpleHTTPServer2.SimpleHTTPRequestHandler.documentRoot = 
        httpServer = SimpleHTTPServer2.HTTPServer( ('', serverPort), 
erver2.SimpleHTTPRequestHandler )
        httpServerWait = httpServer.fileno()
        while self.notifyEvent.isSet():
            httpReady = [httpServerWait], [], [], 1)
            if httpServerWait in httpReady[0]:

David Mitchell wrote:

>Hello group,
>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
>Dave M.

More information about the Python-list mailing list