win32 service and sockets

From: Tom Brown (brown_at_esteem.com)
Date: 02/09/05


To: python-list@python.org
Date: Tue, 8 Feb 2005 16:41:37 -0800

Hi,

I created a win32 service for XPPro called N4010ATestService.py (see below).
The service runs as a particular user with administrative rights. It starts a
thread that creates a simple socket server (N4010ASocketServer.py -- also
below) that just waits for 20 character string. When I run the socket server
by itself the test client can connect to the server and send a 20 character
string just fine. When I run the service, the server will bind to the port
but the client cannot connect. I verified the server was listening on the
given port using netstat -an. The client eventually times out. Why isn't the
server accepting connections when run in a service?

Thanks,
Tom

N4010ATestService.py:
---------------------
import win32serviceutil
import win32service
import win32event
import N4010ASocketServer
from thread import start_new_thread

class N4010ATestService(win32serviceutil.ServiceFramework):
  _svc_name_ = "N4010ATestService"
  _svc_display_name_ = "N4010A Test Service"
  def __init__(self, args):
    win32serviceutil.ServiceFramework.__init__(self, args)

  def SvcDoRun(self):
     start_new_thread(N4010ASocketServer.main, ())
     N4010ASocketServer.waitfor()

  def SvcStop(self):
    # Before we do anything, tell the SCM we are starting the stop process.
    self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
    N4010ASocketServer.stop()

if __name__ == '__main__':
  win32serviceutil.HandleCommandLine(N4010ATestService)

N4010ASocketServer.py:
----------------------
from socket import socket
from select import select
from time import sleep
import win32evtlogutil

applicationName = 'N4010ASocketServer'
messageDll = 'C:\Python24\Lib\site-packages\win32\pythonservice.exe'
running = True
stopped = False

def registerWithEventViewer():
  win32evtlogutil.AddSourceToRegistry(applicationName, messageDll)

def stop():
  import servicemanager
  servicemanager.LogInfoMsg('stopping')
  global running
  running = False

def waitfor():
  while not stopped:
    sleep(0.5)

def main():
  import servicemanager
  registerWithEventViewer()
  servicemanager.LogInfoMsg('creating socket')
  sock = socket()
  servicemanager.LogInfoMsg('binding to port 48777')
  sock.bind(('0.0.0.0', 48777))
  servicemanager.LogInfoMsg('listening')
  sock.listen(5)
  while running:
    servicemanager.LogInfoMsg('Waiting for connection.')
    readyRead, readyWrite, inerror = select([sock], [], [], 0)
    while (not readyRead) and running:
      sleep(0.5)
      readyRead, readyWrite, inerror = select([sock], [], [], 0)
    if running:
      conn, address = sock.accept()
      msg = conn.recv(20)
      servicemanager.LogInfoMsg('Recvd msg: %s' % msg)
      conn.close()
  global stopped
  stopped = True



Relevant Pages

  • RE: call is blocked in recvfrom() and no further proceedings in Win CE
    ... In windows CE, I'm able to send a request but I'm unable to receive it. ... Create another socket & bind with server IP address. ... > My program has to send request to service through port 5070(in this port only ...
    (microsoft.public.windowsce.embedded)
  • Re: Socket, Broadcast et fileevent ?
    ... I slightly modified the IO-Multiplexed Server so that each client's ... # Perl socket stuff is based on, and very similar to, C socket stuff. ... # Use port addresses in the range 5000-6000 for the assignments. ... # When a new connection is established, ...
    (comp.lang.perl.tk)
  • Re: Definition of a socket on Suns website
    ... the server gets a new socket bound to a different port. ... It needs a new socket (and consequently a different port number) so that it can continue to listen to the original socket for connection requests while tending to the needs of the connected client. ...
    (comp.lang.java.programmer)
  • Re: socket communication: socket doesnt connect
    ... Microsoft MVP, MCSD ... As far as your server code goes, ... accept the listening socket. ... overlapped sockets with an I/O Completion Port. ...
    (microsoft.public.vc.language)
  • Re: millionth socket problem....
    ... > my client *is* working. ... > My server is the following, ... > use Socket; ... Your program worked for me, if I changed the port number to 7023, as ...
    (perl.beginners)