javascript - 使用 Apache 时 Flask 找不到应用程序路由
问题描述
我正在尝试运行一个 python 脚本,该脚本获取在 javascript 中收集的数据,在 python 中处理它,然后将其插入数据库。我在 Apache 上运行网页,并且我知道 insertEngagement() 函数可以连接到我的数据库。
但是,当我使用以下 AJAX 请求将数据发送到 python 文件时,app.run() 启动但从未进入 insertNewData() 函数。我猜这与我使用的路径有关?任何帮助,将不胜感激!
这是我在 /home/webpage/script.js 中的 AJAX 请求:
$.ajax({
url: "/home/pythonScripts/processdata.cgi/process",
data: { employeeText: input1, customerText: input2, chatData: otherData},
type: "POST",
success: callbackFunc
});
然后我的python在/home/pythonScripts/processdata.cgi:
#!C:\path\to\python\Python37\python.exe
import sys
import getopt
import cgi
import cgitb; cgitb.enable()
import mysql.connector as conn
#insertdata is another file with functions in it
import insertdata
from flask import Flask, render_template, redirect, url_for,request
from flask import make_response
app = Flask(__name__)
def htmlTop():
print("""Content-type:text/html\n\n
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>My server-side template</title>
</head>
<body>""")
def htmlTail():
print("""</body>
</html>""")
@app.route("/home/pythonScripts/processdata.cgi/process", methods=['GET','POST'])
def insertNewData():
print ("Started insert new data")
if request.method == 'POST':
print ("Inside conditional")
employeeText = request.form['employeeText']
customerText = request.form['customerText']
chatData = request.form['chatData']
# ... use data from request
return "Success"
else:
return "Error"
if __name__ == "__main__":
try:
htmlTop()
app.run(debug=True)
htmlTail()
except:
cgi.print_exception()
此外,我使用 .cgi 是因为我无法将 Apache 配置为运行 python 脚本,而不仅仅是返回文本文件。
解决方案
作为替代答案,如果这不是生产并且您使用的是 Ubuntu 系统,我设法在几天内运行了一个 Flask 应用程序。
这是我遵循的教程的链接;
我知道它没有回答您的问题,但我发现它在第一轮就很有用,并且它没有任何问题,例如使用表单、请求 API 数据和使用 Jinja 模板引擎进行打印。
您还可以使用大量其他开发的插件来进行用户管理和登录、身份验证,但最好的部分是,您可以使用 Python 做的任何事情都可以通过基于 Web 的应用程序进行订购,因此当您掌握它的方式时一切正常,几乎没有你做不到的。
针对这个问题,我个人使用 Gunicorn 作为中间人,使用 Nginx 作为反向代理。但是没有理由 Apache 不能在我知道的那个阵容中替代 Nginx。
推荐阅读
- android - ListView 的样式是否会影响其元素?
- python - 删除数据时间戳并仅每小时获取一次数据python
- swift - 为每个段设置特定的边框
- python - 如何预测价值?
- java - Struts2 从服务器重定向到外部 URL
- javascript - xmlhttprequests 在控制台中工作,但不在脚本中
- c# - 我想要在 if/then 语句中测试的当前“i”
- php - 46/5000 使用 curl php 远程登录并绕过 recaptcha
- javascript - 是否可以同时显示一个 gmail 帐户的多个谷歌日历?
- javascript - 如何将其制作为 HighChart(附图片)