Cannot access Python server running as Windows ser

2019-07-11 05:05发布

问题:

I have written a Python TCP/IP server for internal use, using win32serviceutil/py2exe to create a Windows service.

I installed it on a computer running Windows XP Pro SP3. However, I can't connect to it when it's running as a service. I can confirm that it's binding to the address/port, because I get a conflict when I try to bind to that address/port with another application. Further, I have checked the Windows Firewall settings and have added appropriate exceptions. If I run the server as a simple console application, everything works as expected. However, when I run it as a service, it doesn't work.

I vaguely remember running into this problem before, but for the life of me can't remember any of the details.

Suggestions, anyone?

回答1:

Possibly the program may be terminated just after initialization. Please check whether it is continuously listening to the requests.

netstat -an |find /i "listening"

And analyze the command line parsed to the programs. You may use procexp to do that.



回答2:

First of all, whenever you implement a Windows service, be sure to add proper logging.

My worker threads were terminating because of the exception, "The socket operation could not complete without blocking."

The solution was to simply call sock.setblocking(1) after accepting the connection.



回答3:

Check to see that the service is running under the Nertwork Service account and not the Local System account. The later doesn't have network access and is the default user to run services under. You can check this by going to the services app under administrative tool in the start menu and looking for your service. If you right-click the service you can go to properties and change the user that it is run under.