1

我想从与烧瓶python脚本位于同一文件夹中的外部python脚本调用一个函数。加载服务器时,我想在按下按钮时启动“QEFtest.py文件的testfunc” html页面。任何人都可以在这个问题上帮助我我尝试了几种方法,但没有奏效。

import flask,flask.views
import functools
import spirentQEF
import subprocess
import sys 

app = flask.Flask(__name__)
app.secret_key = "bacon"

users ={'jake':'bacon','root':'root'}
class Main(flask.views.MethodView):
    def get(self):
        return flask.render_template('index.html')

    def post(self):
        if 'logout' in flask.request.form:
            flask.session.pop('username', None)
            return flask.redirect(flask.url_for('index'))
        required = ['username', 'passwd']
        for r in required:
            if r not in flask.request.form:
                flask.flash("Error: {0} is required.".format(r))
                return flask.redirect(flask.url_for('index'))
        username = flask.request.form['username']
        passwd = flask.request.form['passwd']
        print('username  %s'%username)
        print('passwd %s' %passwd)
        if username in users and users[username] == passwd:
            flask.session['username'] = username
#             print('username %s'%username)
#             print('passwd %s' %passwd)
        else:
            flask.flash("Invalid Username or incorrect Password")
        return flask.redirect(flask.url_for('index'))
                
def login_required(method):
    @functools.wraps(method)
    def wrapper(*args, **kwargs):
        if 'username' in flask.session:
            return method(*args, **kwargs)
        else:
            flask.flash("You need to login to visit this link ! ")
        return flask.redirect(flask.url_for('index'))
    return wrapper
    
    

# 远程html页面的代码

class Remote(flask.views.MethodView):
    @login_required
    def get(self):
        return flask.render_template('remote.html')
#         proc = subprocess.Popen([sys.executable,"QEFtest.py"])
#         proc.communicate()

    @login_required     
    def post(self):
        result = str(flask.request.form['expression'])
        flask.flash("The Modcod which is is being tested : ")
        flask.flash(result)
        flask.flash("Test is running, and we are  in the middle of testing one MODCOD. Please check back later")
        print('result :  %s' %result)
        return flask.redirect(flask.url_for('remote'))
        return subprocess.call()
        print('result  :  %s' %result)
        print("its working")

单击按钮时,我想在此处启动 QEFtestfunc.py

proc = subprocess.Popen([sys.executable,"QEFtest.py"])
proc.communicate()


class page(flask.views.MethodView):
    @login_required
    def get(self):
        return flask.render_template('third.html')

    @login_required
    def post(self):
        res = str(flask.request.form['expression'])
        flask.flash("The modcod which is being tested :")
        flask.flash(res)
        flask.flash("The test is in the final stage please wait for few minutes")
        print('result : %s' %res)
        return flask.redirect(flask.url_for('page'))
        print('result :  %s' %res)

x = QEFtest.testFunc(999,
              9,
              0.9,
              100000000,
              60.0,
              0.0,
             0.1,
             "10.169.6.74",
              3,
              3,
             1,
              2,
              str("C:/testdir/webservertest"))
app.add_url_rule('/',view_func=Main.as_view('index'),methods=['GET', 'POST'])
print("one")
print(str(__name__))
app.add_url_rule('/remote/',view_func=Remote.as_view('remote'), methods=['GET', 'POST'])
app.add_url_rule('/page/',view_func=page.as_view('page'), methods=['GET', 'POST'])
print("two")
app.debug = True
print("three")
app.run()


if __name__ == '__main__':

    print("its working")
    x = QEFtest.testFunc(999,
              9,
              0.9,
              100000000,
              60.0,
              0.0,
             0.1,
             "10.169.6.74",
              3,
              3,
             1,
              2,
              str("C:/testdir/webservertest"))
#return str(flask.request.form['expression'])

我不明白如何在服务器运行时启动另一个 python 文件的功能。单击按钮时,该功能很少进行计算。

4

0 回答 0