2017-07-12 92 views
0

Flaskで小さなアプリを構築して、複数のIPベースのデバイスを再起動します。私はデバイスのチェックリストを持っていきたいと思っています。それが送信されると、ip/rebootpage.htmlが開きます。現時点では、私のコードは/ rebootpageという形式のデータをすべて組み合わせようとしています。ここで私はこれまで持っているものです。Flask、WTFormsが新しいタブで複数のURLを開く

app.py

from flask import Flask, render_template, redirect 
from flask_wtf import FlaskForm 
from wtforms import widgets,SelectMultipleField 

app = Flask(__name__) 
app.config['SECRET_KEY'] = "565&SDdsa7fgSdst7%6" 

Test_Choices = [('10.202.214.196', '#61'), ('10.202.214.197', '#62')] 
Test_Choices_NR = [('10.202.214.198', 'Net Relay 1')] 


class RebootForm(FlaskForm): 
    available = SelectMultipleField('Available', choices=Test_Choices, 
            option_widget=widgets.CheckboxInput(), 
            widget=widgets.ListWidget(prefix_label=False)) 
    availableNR = SelectMultipleField('Available Net Relays', choices=Test_Choices_NR, 
             option_widget=widgets.CheckboxInput(), 
             widget=widgets.ListWidget(prefix_label=False)) 


@app.route('/form', methods=['GET', 'POST']) 
def form(): 
    form = RebootForm() 
    if form.validate_on_submit(): 
     list = '{}'.format(form.available.data).replace("'", "").replace("[", "").replace("]", "") 
     for each in list: 
      return redirect('http://{}/rc.cgi?L=uirreboot.html&c=99'.format(each)) 
    return render_template('form.html', form=form) 

if __name__ == '__main__': 
    app.run(debug=True) 
+0

は、実際に、それはあなたがどのように達成し、しようとするかを理解するのは難しいです。あなたはあなたの質問を書き直せますか? – Fian

答えて

0

IRC周りの少しの提示が私の答えに私を得ました。答えは私が要求を使用しなければならないことです(要求ではなく、2つの異なるものがあります)。私の最終的なコードはこのように見え、素晴らしい作品です。要求はページを開くことなく要求を作成することに注意してください。

app.py

from flask import Flask, render_template, redirect, url_for 
import requests 
from flask_wtf import FlaskForm 
from wtforms import widgets,SelectMultipleField 

app = Flask(__name__) 
app.config['SECRET_KEY'] = "565&SDdsa7fgSdst7%6" 

All_Selected = [('Everything', 'Entire Site')] 
Test_Choices = [('10.202.214.196', '#61'), ('10.202.214.197', '#62')] 
Test_Choices_NR = [('10.202.214.198', 'Net Relay 1')] 


class RebootForm(FlaskForm): 
    all_selected = SelectMultipleField('Select All', choices=All_Selected, 
             option_widget=widgets.CheckboxInput(), 
             widget=widgets.ListWidget(prefix_label=False)) 
    available = SelectMultipleField('Available', choices=Test_Choices, 
            option_widget=widgets.CheckboxInput(), 
            widget=widgets.ListWidget(prefix_label=False)) 
    availableNR = SelectMultipleField('Available Net Relays', choices=Test_Choices_NR, 
             option_widget=widgets.CheckboxInput(), 
             widget=widgets.ListWidget(prefix_label=False)) 


@app.route('/form', methods=['GET', 'POST']) 
def form(): 
    form = RebootForm() 
    ugly_messages = [] 
    if form.validate_on_submit(): 
     ip_addresses = form.available.data 
     for ip_address in ip_addresses: 
      try: 
       requests.get('http://{}/rc.cgi?L=uirreboot.html&c=99'.format(ip_address)) 
       ugly_messages.append('rebooting {}'.format(ip_address)) 
       ugly_messages.append('Please wait 30 secs.') 
      except Exception: 
       ugly_messages.append('{} did not reboot. It may be offline.'.format(ip_address)) 
     ip_addressesNR = form.availableNR.data 
     for ip_addressNR in ip_addressesNR: 
      try: 
       requests.get('http://{}/setup.cgi?L=uireboot2.html&R'.format(ip_addressNR)) 
       ugly_messages.append('rebooting {}'.format(ip_addressNR)) 
       ugly_messages.append('Please wait 30 secs.') 
      except Exception: 
       ugly_messages.append('{} did not reboot. It may be offline.'.format(ip_addressNR)) 
     return "<br/>".join(ugly_messages) 
    return render_template('form.html', form=form) 


if __name__ == '__main__': 
    app.run(debug=True) 
関連する問題