在Windows中将Python Web服务器作为服务运行
我有一个小的Web服务器应用程序,我用
Python编写,从数据库系统获取一些数据并将其作为XML返回给用户.这部分工作正常 – 我可以从命令行运行Python Web服务器应用程序,我可以让客户端连接到它并获取数据.目前,要运行Web服务器,我必须以管理员用户身份登录到我们的服务器,并且必须手动启动Web服务器.我希望Web服务器在系统启动时自动启动为服务并在后台运行.
使用ActiveState的网站和StackOverflow的代码,我非常清楚如何创建服务,我想我已经有点排序 – 我可以安装和启动我的Web服务器作为Windows服务.但是,我无法弄清楚如何再次停止服务.我的Web服务器是从BaseHTTPServer创建的: server = BaseHTTPServer.HTTPServer(('',8081),SIMSAPIServerHandler) server.serve_forever() 自然地,serve_forever()调用使Web服务器处于无限循环中并等待HTTP连接(或ctrl-break按键,对服务无用).我从上面的示例代码中得到了一个想法,即你的main()函数应该处于一个无限循环中,只有当它处于“停止”状态时才会突破它.我的主要调用serve_forever().我有一个SvcStop功能: def SvcStop(self): self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) exit(0) 当我从命令行执行“python myservice stop”时,我似乎被调用了(我可以在其中放置一个产生输出到文件的调试行)但实际上并没有退出整个服务 – 后续调用“python myservice start” “给我一个错误:
随后的停止呼叫给了我:
我想我需要替换serve_forever(serve_until_stop_received,或其他),或者我需要一些修改SvcStop的方法,以便它停止整个服务. 这是一个完整的列表(我已经修剪了包含/注释以节省空间): class SIMSAPIServerHandler(BaseHTTPServer.BaseHTTPRequestHandler): def do_GET(self): try: reportTuple = self.path.partition("/") if len(reportTuple) < 3: return if reportTuple[2] == "": return os.system("C:ProgramsSIMSAPIrunCommandReporter.bat " + reportTuple[2]) f = open("C:ProgramsSIMSAPIout.xml","rb") self.send_response(200) self.send_header('Content-type',"application/xml") self.end_headers() self.wfile.write(f.read()) f.close() # The output from CommandReporter is simply dumped to out.xml,which we read,write to the user,then remove. os.unlink("C:ProgramsSIMSAPIout.xml") return except IOError: self.send_error(404,'File Not Found: %s' % self.path) class SIMSAPI(win32serviceutil.ServiceFramework): _svc_name_ = "SIMSAPI" _svc_display_name_ = "A simple web server" _svc_description_ = "Serves XML data produced by SIMS CommandReporter" def __init__(self,args): win32serviceutil.ServiceFramework.__init__(self,args) self.hWaitStop = win32event.CreateEvent(None,None) def SvcStop(self): self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) exit(0) def SvcDoRun(self): import servicemanager servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,servicemanager.PYS_SERVICE_STARTED,(self._svc_name_,'')) self.timeout = 3000 while 1: server = BaseHTTPServer.HTTPServer(('',SIMSAPIServerHandler) server.serve_forever() def ctrlHandler(ctrlType): return True if __name__ == '__main__': win32api.SetConsoleCtrlHandler(ctrlHandler,True) win32serviceutil.HandleCommandLine(SIMSAPI) 解决方法
这就是我做的:
我没有直接实例化BaseHTTPServer.HTTPServer类,而是编写了一个发布“stop”方法的新后代: class AppHTTPServer (SocketServer.ThreadingMixIn,BaseHTTPServer.HTTPServer): def serve_forever(self): self.stop_serving = False while not self.stop_serving: self.handle_request() def stop (self): self.stop_serving = True 然后,在你已经拥有的方法SvcStop中,我调用该方法来打破serve_forever()循环: def SvcStop(self): self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) self.httpd.stop() (self.httpd是实现Web服务器的AppHTTPServer()实例) 如果你在后台线程上正确使用setDaemon(),并正确中断服务中的所有循环,那么指令 exit(0) 在SvcStop()中不应该是必需的 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 如何在Windows上安装和使用gpg-agent?
- .net – Windows Azure与Amazon EC2
- .NET 3.5是否允许使用Windows 7的新功能?
- 尝试在Windows上安装Python bcrypt的许多问题(x6
- Windows API的CreateFile()中的OPEN_ALWAYS和CRE
- windows-server-2003 – 无法使用w32tm / stripc
- 如何从Windows批处理文件运行Java但隐藏命令窗口
- Windows – 如何创建批处理文件来重命名文件夹中
- winapi – 如何从Powershell事件订阅者操作??设置
- windows – inno setup需要有关开始菜单下图标组