Reputation: 564
I've written a simple HTTP server and made it into a Windows service using pywin32. The server successfully processes requests when run in the debugger, inside actual service it gets the request but hangs on send_response operation. What might be the reason?
from http.server import BaseHTTPRequestHandler, HTTPServer
import win32serviceutil
import win32service
import sys
PORT_NUMBER = 6363
class myHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bytes("Hello World !", "utf-8"))
return
class TestSvc(win32serviceutil.ServiceFramework):
_svc_name_ = "TestSvc"
_svc_display_name_ = "Test Service"
_svc_description_ = "Tests server inside service."
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.server = HTTPServer(('', PORT_NUMBER), myHandler)
def SvcDoRun(self):
self.ReportServiceStatus(win32service.SERVICE_RUNNING)
print('Started httpserver on port ', PORT_NUMBER)
self.server.serve_forever()
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
self.server.shutdown()
#sys.frozen = 'windows_exe'
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(TestSvc, argv=sys.argv)
Upvotes: 1
Views: 743
Reputation: 564
Actually it was hanging when executing sys.stderr.write (which is the default logging output for BaseHTTPRequestHandler). So I've overridden log_message function in my request handler class and it works fine now.
Upvotes: 3