apache未投放除"/"以外的烧瓶路线 [英] Flask routes other than '/' not being served by apache

查看:83
本文介绍了apache未投放除"/"以外的烧瓶路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我首先要说我是Flask,Apache和Web服务的新手,因此我为自己的无知向您道歉.

So I should preface this by saying I am new to Flask, Apache, and web serving so I apologize in advance for my ignorance.

我正在尝试使用Raspberry Pi 3建立一个远程数据记录项目.我正在将数据捕获到SQL数据库中,然后以交互式绘图的形式通过Apache提供该信息.

I am trying to set up a remote data logging project with Raspberry Pi 3. I am capturing data to a SQL database then serving that info via Apache in the form of interactive plots.

最近我一直在尝试用烧瓶做事,因为我喜欢它的灵活性.我已经成功设置了flask和Apache,并且都可以使用hello世界.然后,我设置了wsgi以通过Apache服务于烧瓶路由,并且能够使hello world正常工作.然后,我复制了此示例(

I have been trying to do things with flask lately as I like how flexible it is. I have successfully setup flask and Apache and have both serving hello worlds. I then setup wsgi to serve the flask routes via Apache and was able to get hello world to work. I then replicated this example ( PART 1 / PART 2 ) with success.

然后,我想将重新启动按钮连接到实际上可以重新启动Pi的烧瓶路径.我能够使它在开发环境'localhost:5000'中工作.相反,如果我从Web导航到Pi,则可以加载sysinfo页面,但是重新启动的路径失败,并显示404错误.

I then wanted to connect the restart button to a flask route that would actually restart the Pi. I am able to get this to work in the dev environment 'localhost:5000'. If instead I navigate to the Pi from the web I am able load the sysinfo page but the route to the restart fails with a 404 error.

未找到在服务器上找不到所请求的URL.如果您 手动输入网址,请检查您的拼写,然后重试."

"Not Found The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again."

以下是从index.py出发的路线:

Below is the route from index.py:

@app.route("/sysinfo/restart_now", methods=['GET','POST'])
def restart_now():
    subprocess.call("delayed-reboot",shell=True)
    return render_template("restart_now.html")

在index.html模板中提供用于路由的按钮代码:

And the button code serving the route in the index.html template:

     <form action="/sysinfo/restart_now" method="post">
    <button name="restart_now" onclick="return confirm('Do you wish to shutdown your system ?')" class="button button-primary">Reboot Pi</button>
     </form>

再加上restart_now.html模板中的php倒计时:

And the php countdown in the restart_now.html template:

  <p>Sysinfo will reload automatically in <span id="counter">45</span> second(s).</p>
  <script type="text/javascript">
  function countdown() {
      var i = document.getElementById('counter');
      if (parseInt(i.innerHTML)<=0) {
      location.href = '/sysinfo';

      }
      if (parseInt(i.innerHTML)>0) {
      i.innerHTML = parseInt(i.innerHTML)-1;

      }            
  }
  setInterval(function(){ countdown(); },1000);
  </script>

这是我放置在/usr/local/bin中的bash脚本延迟重启:

this is the bash script delayed-reboot which I have placed in /usr/local/bin:

#!/bin/bash
( sleep 5 ; sudo reboot ) &

这是我正在使用的wsgi配置:

and this is the wsgi configure I am using:

WSGIDaemonProcess sysinfo user=pi group=www-data threads=5
WSGIScriptAlias /sysinfo /var/www/sysinfo/sysinfo.wsgi
<Directory "/var/www/sysinfo">
    WSGIProcessGroup sysinfo
    WSGIScriptReloading On
    WSGIApplicationGroup %{GLOBAL}
    Require all granted
</Directory>
ErrorLog /var/www/sysinfo/logs/error.log

和sysinfo.wsgi文件:

and the sysinfo.wsgi file:

import sys
sys.path.insert(0, '/var/www/sysinfo')
from index import app as application

任何帮助您理解我在做什么的人,将不胜感激.

Any help understanding what I am doing wrong would be greatly appreciated.

-谢谢

推荐答案

它不起作用,因为您将应用程序安装在Apache中的子URL上,但是在Flask中的路由中也包含了安装点.这意味着您使用的URL必须为/sysinfo/sysinfo/restart_now.

It isn't working because you are mounting the application at a sub URL in Apache, but your route in Flask also has the mount point in it. This means the URL you use would need to be /sysinfo/sysinfo/restart_now.

您只需要在Flask路由中使用/restart_now,或者将Apache配置更改为使用:

You either need to use in the Flask route just /restart_now, or change the Apache configuration to use:

WSGIScriptAlias /sysinfo /var/www/sysinfo/sysinfo.wsgi/sysinfo

在最后一个参数上添加尾随/sysinfo会使事情变得混乱,以便Flask应用程序仍将挂载点视为PATH_INFO的一部分,这是Flask进行的选择.

The addition of the trailing /sysinfo on last argument fiddles things so that the Flask application still sees the mount point as part of PATH_INFO, which is what Flask routes off.

顺便说一句,您不需要WSGIScriptReloading On,因为无论如何这是默认设置.

BTW, you don't need WSGIScriptReloading On as that is the default anyway.

这篇关于apache未投放除"/"以外的烧瓶路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆