我理解本书中的例子" Flask Web Development"然而,我正在尝试做一些有点奇怪的事情。我希望能够点击' part'像这样的图像 - > http://www.w3schools.com/tags/tag_area.asp
但是我希望它打开一个SSH窗口到我网络中的特定节点 我通过一个单独的页面按照示例使用它。如果我打开localhost:5000 / leaf1这是完美的,但是我想从链接传递一个cgi-bin POST命令,以便用户仍然在同一页面上(它不会转到另一个页面......)。我有点卡住了。
这是开始烧瓶的python脚本:
import subprocess
import logging
from logging.handlers import RotatingFileHandler
from flask import Flask, render_template, request, url_for
from flask.ext.script import Manager
from flask.ext.bootstrap import Bootstrap
from flask.ext.moment import Moment
from flask.ext.wtf import Form
from wtforms import StringField, SubmitField, HiddenField
from wtforms.validators import Required
app = Flask(__name__)
app.config['SECRET_KEY'] = 'hard to guess string'
manager = Manager(app)
bootstrap = Bootstrap(app)
moment = Moment(app)
class SwitchForm(Form):
name = HiddenField('Switch being accessed', validators=[Required()])
@app.route("/leaf1")
def leaf1():
cmd = ['DISPLAY=:03.0 xterm -e "ssh leaf1"']
p = subprocess.Popen(cmd, stdout = subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE, shell=True)
out,err = p.communicate()
return out
@app.route('/', methods=['GET', 'POST'])
def index():
leaf=None
if request.method == 'POST':
switch=request.form['leaf']
if switch == '1':
return ''' hello guy '''
return render_template('index.html')
if __name__ == '__main__':
handler = RotatingFileHandler('log.log', maxBytes=10000, backupCount=1)
handler.setLevel(logging.INFO)
app.logger.addHandler(handler)
manager.run()
这是index.html模板文件
{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block title %}Cumulus Workbench{% endblock %}
{% block page_content %}
<img src="{{ url_for('static', filename='wbench.png') }}" usemap="#switchmap">
<map name="switchmap">
<form method="post" action="index.html" id="form1">
<input type="hidden" name="leaf" value="1" />
<area shape="rect" coords="69,226,181,257" href="index.html" onclick="document.getElementById('form1').submit(); return false;">
</form>
<area shape="rect" coords="421,226,534,257" href="index.html">
</map>
{% endblock %}