Python 从flask中的用户输入运行脚本
我正在使用flask为我编写的脚本创建一个网页,并希望请求用户输入,无论他们输入的内容是在我编写的python脚本中运行的,我如何才能做到这一点 这是我创建的烧瓶页面Python 从flask中的用户输入运行脚本,python,flask,Python,Flask,我正在使用flask为我编写的脚本创建一个网页,并希望请求用户输入,无论他们输入的内容是在我编写的python脚本中运行的,我如何才能做到这一点 这是我创建的烧瓶页面 from flask import Flask, render_template, redirect, request, url_for #from flask_script import Manager import json import connect import requests from elasticsearch i
from flask import Flask, render_template, redirect, request, url_for
#from flask_script import Manager
import json
import connect
import requests
from elasticsearch import Elasticsearch
app = Flask(__name__)
def elastic_search(text):
es = Elasticsearch(['10.0.0.9:9200', '10.0.0.15:9200'])
word = input(text)
res = es.search(index="pastebin-*", doc_type='doc', body={"query": {"bool": {"must": [{"match": {"key": word}}]}}})
return res
@app.route('/key', methods=['POST', 'GET'])
def key():
if requests.method == 'POST', 'GET':
result = request.form['key']
elastic_search(result)
return render_template('key.html')
@app.route('/result', methods=['POST', 'GET'])
def result():
if request.method == 'POST':
result = request.form
return render_template('result.html', result = result)
if __name__ == '__main__':
app.run('0.0.0.0')
app.run(threaded=True)
这是HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>Key</title>
<link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="styleshee$>
<link href="http://getbootstrap.com/examples/jumbotron-narrow/jumbotron-narrow.css" rel="stylesheet">
<link href="../static/key.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="header">
<h3 class="text-muted"><Key/h3>
</div>
<div class="jumbotron">
<h1>Please Enter Pastebin Key</h1>
<form class="form-signin">
<form action="" method"post">
<label for="Key" class="sr-only">Key</label>
<input type="text" name="inputKey" id="inputKey" class="form-control" placeholder="Password" required value="{{
request.form.key }}">
<input type="submit" value="Submit">
<<button id="btnEnter" class="btn btn-lg btn-primary btn-block" type="submit" formmethod="post">Enter</button>
</form>
{% if error %}
<p class="error"><strong>Error:</strong> {{ error }}
{% endif %}
</div>
<footer class="footer">
<p>© Point</p>
</footer>
</div>
</body>
</html>
钥匙
将脚本更改为函数。并在用户在浏览器中输入内容时调用该函数
def elastic_search(text):
word = input(text)
res = es.search(index="pastebin-*", ...)
return res
# View function
@app.route('/key', methods=['POST', 'GET'])
def key():
if requests.method == 'POST':
result = request.form['key']
resp = elastic_search(result)
return render_template('key.html', resp=resp)
当我运行它时,我似乎可以工作,但当我尝试访问它时,我得到了404。可能是html吗?请解释您希望在html中看到什么以及什么不起作用。如果你解释清楚的话,我可以解释这个行为。我想要一个页面,其中有一个输入框和enter按钮,用户输入框,然后当他们按enter键时,它搜索脚本OK,我看到了。您的HTML中有inputkey
,但在视图函数中,您试图从请求中获取此key
。你确定这是正确的吗?所以它应该说key而不是inputkey?
def elastic_search(text):
word = input(text)
res = es.search(index="pastebin-*", ...)
return res
# View function
@app.route('/key', methods=['POST', 'GET'])
def key():
if requests.method == 'POST':
result = request.form['key']
resp = elastic_search(result)
return render_template('key.html', resp=resp)