运行Python作为服务时出现“404 Not Found”错误

时间:2017-11-20 11:43:37

标签: python windows flask service

我开发了一个HTML网页,数据由Flask提供的Python代码提供。我试图使用win32service包在Windows中将此烧录代码作为服务运行。 (在Python3.6上编码) 当我通过命令:python tryflask.py debug在cmd上运行烧瓶代码时,它显示在IP上运行的页面,其中包含端口号等(在http://0.0.0.0:5000上运行)。但网页上没有显示任何内容。我收到错误:404 Not Found。 我哪里错了?另外,我认为它没有在烧瓶代码中输入“def slides()”函数。

以下是烧瓶计划。

import flask
from flask import Flask, render_template, redirect
from extract import *
import sys
from flask import request
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket

app = Flask(__name__)
@app.route('/slideshow')
def slides():
   print ("Hello")          //Not getting printed
   ext_obj=extract_news()
   hntop = ext_obj.hntop()
   br=ext_obj.breaking()

   ext_tweet= ext_obj.extract_tweet()
   tweet1=ext_obj.tweet1()
   tweet2=ext_obj.tweet2()
   return render_template('exampleslide.html', hntop=hntop, br=br, tweet1=tweet1, tweet2=tweet2, )

@app.route('/newspage', methods = ['GET','POST'])
def loadhome():
   if request.method == 'POST':
      ext_obj=extract_news()
      hn=ext_obj.hacker_news()
      tp=ext_obj.hacker_news()
      return render_template('home.html',tp=tp,hn=hn)

def start_flask():
    print("Starting flask")      //Printing
    app.run('0.0.0.0',port=5000)

class AppServerSvc (win32serviceutil.ServiceFramework):
    _svc_name_ = "newsslides"
    _svc_display_name_ = "newsslides"

    def __init__(self,args):
        win32serviceutil.ServiceFramework.__init__(self,args)
        self.hWaitStop = win32event.CreateEvent(None,0,0,None)
        socket.setdefaulttimeout(6000)

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)

    def SvcDoRun(self):
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                              servicemanager.PYS_SERVICE_STARTED,
                              (self._svc_name_,''))
        self.ReportServiceStatus(win32service.SERVICE_RUNNING)
        print ("Starting service")     //Printing
        start_flask()

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

非常感谢任何帮助!提前谢谢!

1 个答案:

答案 0 :(得分:1)

运行Windows服务时,DoRun函数永远不会返回,否则服务将基本停止。将其添加到start_flask方法的底部:

while True: 
    time.sleep(30)