python - 关于将大量脚本部署到 Web 服务器(Java、Python)的建议

标签 python linux http web flask

目前,我正在编写一个数据处理工作流程,并尝试将其部署在整个工作流程中的 Web 服务器上。我必须使用两种不同的语言(Java 和 Python)。 我有一个 HTML 表单,它接受用户的输入,然后我的 python Flask 应用程序将处理该输入并使用 python 中内置的调用函数将其传递给 java 程序。然后我的 flask 应用程序将获取输出,再次处理它,并将其返回给用户。

我能够在本地 Linux 系统上完美运行整个工作流程,我打开 HTML 表单,填写输入并提交,然后浏览器将我重定向到结果页面,没有任何问题。但是,当我尝试在我们实验室使用的文字新闻服务器上部署整个管道时,我无法使其工作。我仍然可以运行 Flask 应用程序,但它根本不监听 HTML 表单上的 post 请求。我已附上我的 Flask 应用程序和 HTML 代码。是不是我哪里没设置好?或者是否有某种系统,我可以使用它在本地设置整个系统并将管道一次性部署到服务器上?我一直在寻找 python Flask 部署建议,但其中大多数都让我非常困惑。如果有人能给我一些关于将此管道部署到服务器上的有用建议,那就太好了。如有任何建议,我们将不胜感激!

<form action = "http://localhost:5000/login" method = "POST" 
    enctype="multipart/form-data">
    <fieldset>
    <legend>Plot Config:</legend>
    <br>Please upload your input file:<br>
    <input type="file" name="file" accept="*/*">    
    <br><br>	
	

    <br>Classifer type:<br>
    <input type="radio" name="classifier" value="SVM" checked>Support Vector Machine<br>
    <input type="radio" name="classifier" value="NB">Naive Bayes<br>
    <input type="radio" name="classifier" value="RF">Random Forest<br>

    <br>Number of estimators (if you chose Random Forest as your classifier):<br>
    <input type="text" name="estimators" value="10"><br>	


    <br>Would you like to average your result?:<br>
    <input type="radio" name="avg" value="Yes" checked>Yes<br>
    <input type="radio" name="avg" value="No"> No<br>	
    

    <br>Feature Selection interval:<br>
    <input type="text" name="interval" value=10><br>	


    <br>Plot feature range:<br>
    <input type="text" name="plotrange" value=85><br>

    <br>Plot lengend size:<br>
    <input type="text" name="legendsize" value=7.5><br>    
    	  		
    <br>Plot line width:<br>
    <input type="text" name="plotlinewidth" value=2><br> 

    <br>Legend title:<br>
    <input type="text" name="legendtitle"><br>








<br><br>	
    <input type="reset">
    <input type="submit" value="Submit">

    

  </fieldset>
</form>

from flask import Flask, redirect, url_for, request,send_file
import configparser
from werkzeug import secure_filename
import os
from subprocess import call

app = Flask(__name__)

@app.route('/success/<name>')
def success(name):
    return 'welcome! %s' % name

@app.route('/get_image')
def get_image():
    if request.args.get('type') == '1':
       filename = 'download.jpg'
    else:
       filename = 'download.jpg'
    return send_file(filename, mimetype='image/jpg')    

@app.route('/login',methods = ['POST', 'GET'])
def login():
    if request.method == 'POST':
        f=request.files['file']
        workdir=os.path.join(os.getcwd(), 'Input(LeaveOneOut)',f.filename)
        print workdir     
        f.save(workdir)
        classifier = request.form['classifier']
        estimators=request.form['estimators']
        avg=request.form['avg']
        interval=request.form['interval']
        plotrange=request.form['plotrange']
        legendsize=request.form['legendsize']
        plotlinewidth=request.form['plotlinewidth']
        legendtitle=request.form['legendtitle']
        testoutput=classifier+" "+estimators+" "+avg+" "+interval+" "+plotrange+" "+legendsize+" "+plotlinewidth+" "+legendtitle        
        settings = configparser.ConfigParser()
        settings._interpolation = configparser.ExtendedInterpolation()
        settings.read('LeaveOneOutConfig.txt')
        settings.set('SectionOne', 'Classifier', str(classifier))   
        settings.set('SectionOne', 'number of estimators', str(estimators))
        settings.set('SectionOne', 'average the result', str(avg))
        settings.set('SectionOne', 'feature selection interval', str(interval)) 
        settings.set('SectionOne', 'plot feature range', str(plotrange))
        settings.set('SectionOne', 'plot lengend size', str(legendsize)) 
        settings.set('SectionOne', 'plot line width', str(plotlinewidth)) 
        settings.set('SectionOne', 'dataset type name', str(legendtitle))         
        call(["python", "LeaveOneOut.py"])





        with open('LeaveOneOutConfig.txt', 'wb') as configfile:
            settings.write(configfile)        

        return redirect(url_for('get_image'))
        return redirect(url_for('success',name =testoutput ))
if __name__ == '__main__':
    app.run(debug = True)

最佳答案

所以我最终发现,如果您希望内容公开,您需要使服务器在 0.0.0.0 和端口 80 上运行。 就我而言,我将最后一行更改为 app.run("0.0.0.0", port=80)。另外,Flask 连接的服务器似乎相当不稳定,这实际上是在 Flask 文档中写的,您应该使用一些其他部署选项。我用的是gunicorn,它很容易使用,而且对我来说很稳定。如果有人遇到同样的问题,希望对您有所帮助。

关于python - 关于将大量脚本部署到 Web 服务器(Java、Python)的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45197793/

相关文章:

python - 如何在 Python 脚本中提供输入答案

python - 有没有办法使用 Anaconda 作为与 npm 相同的包管理器?

python - 使用 matplotlib 在图例中制作自定义垂直线标记

http - 我的 URL 是否违反了 RFC 3986(或其他)

javascript - 对垃圾收集的 HTTP 请求的 HTTP 响应会发生什么?

python - 为什么这段代码只有在输入全是大写字母时才有效?

java - 导出运行 JavaFX Scene3D 的 X Display

linux - 如何监视文本文件中连续的某些行

android - Android 是否支持 HUP 信号生成/处理?

ios - 发送帖子请求 IOS 7