API在IIS服务器中不工作,但在localhost、Python Flask中工作

i86rm4rw  于 2023-10-19  发布在  Python
关注(0)|答案(1)|浏览(108)

我已经在IIS服务器上部署了一个Python Flask应用程序。当我浏览主URL(http://<myserverip>:8080/)时,它工作得很好。然而,当我尝试路由到不同的URL或使用'GET'或'POST'(例如:http://<myserverip>:8080/process),我会得到
此错误(HTTP 404 Not Found)表示Internet Explorer能够连接到网站,但未找到所需的页面。很可能网页暂时不可用。或者,该网站可能已更改或删除该网页

  • 但是在localhost下运行正常。* 我不确定IIS Sever上是否有我遗漏的配置,因为它在localhost上运行。

下面是我的示例代码:

  • main.py*
from website import create_app

app = create_app()

if __name__ == '__main__':
    from waitress import serve
    serve(app)
  • web.config*
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="PythonHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" requireAccess="Script"/>
    </handlers>
    <httpPlatform processPath="C:\Python\Python39\python.exe"
                  arguments="-m waitress --port %HTTP_PLATFORM_PORT% wsgi:application"
                  stdoutLogEnabled="true"
                  stdoutLogFile="c:\home\LogFiles\python.log"
                  startupTimeLimit="60"
                  processesPerApplication="16">
      <environmentVariables>
        <environmentVariable name="SERVER_PORT" value="%HTTP_PLATFORM_PORT%" />
      </environmentVariables>
    </httpPlatform>
  </system.webServer>
</configuration>
  • view.py* 示例
from flask import Blueprint, render_template, request,jsonify,redirect,url_for,session
from json import dumps
import pandas as pd

views = Blueprint('create_flow_chart', __name__)

@views.route('/')
def home():

    <do something>
    return render_template("create-flow-chart.html",category=category, steps =dumps(text),product=product)

@views.route('/process', methods=['POST','GET'])
def send_selection():

    <dosomething>

    return redirect(url_for('.do_create',steps=steps))

@views.route('/create')
def do_create():

  <dosomething>
    
    return render_template("create-flow-chart.html",category=category, steps = dumps(steps),product=product)
kpbpu008

kpbpu0081#

我不知道为什么会发生这种情况,但幸运的是,我能够解决它。我通过重组我的代码解决了它。。我没有使用main.py,而是使用views.py来运行我的应用程序,我认为我使用Blueprint的方式是错误的。
这是我的样本view.py

from flask import Blueprint, render_template, request,jsonify,redirect,url_for,session,Flask
from json import dumps
import pandas as pd

app = Flask(__name__)

@app.route('/')
def home():

# do something    

    return render_template("create-flow-chart.html",category=category, steps =dumps(text),product=product)

@app.route('/process', methods=['POST','GET'])
def send_selection():

# do something
    return redirect(url_for('.do_create',steps=steps))

@app.route('/create')
def do_create():
# do something

    return render_template("create-flow-chart.html",category=category, steps = dumps(steps),product=product)

if __name__ == "__main__":
    from waitress import serve
    serve(app)

相关问题