【问题标题】:'404 Not Found' error while running Python as a service将 Python 作为服务运行时出现“404 Not Found”错误
【发布时间】:2017-11-21 17:20:36
【问题描述】:

我开发了一个 HTML 网页,其数据由通过 Flask 提供的 Python 代码提供。我正在尝试使用 win32service 包在 Windows 中将此烧瓶代码作为服务运行。 (在 Python3.6 上编码) 当我通过命令在 cmd 上运行烧瓶代码时:python tryflask.py debug,它显示在 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)

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

【问题讨论】:

  • 如果你转到http://0.0.0.0:5000/slideshow会发生什么
  • @Ken:我收到以下错误:未找到服务器上未找到请求的 URL。如果您手动输入了 URL,请检查您的拼写并重试。
  • http://127.0.0.1:5000/slideshow怎么样
  • 当您运行它不是作为 Windows 服务时它是否工作?您是否在防火墙中打开了 5555 端口?
  • 服务是在你启动后实际运行的,还是启动后停止的?尝试将while self.run == True: time.sleep(30) 放入 start_flask 方法中。

标签: python windows flask service


【解决方案1】:

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

while True: 
    time.sleep(30)

【讨论】:

    猜你喜欢
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    • 2016-10-05
    • 2015-08-11
    • 2019-12-09
    • 2016-04-10
    相关资源
    最近更新 更多