On my page, I have 5 different text boxes, and I would like when the user submit button, the text-box with the data in it gets posted.
PYTHON:
from flask import Flask, request, redirect, url_for, render_template
from flask_wtf import Form
from wtforms import StringField
from wtforms.validators import InputRequired
with open('iwlist/sep1.txt', 'r') as infile:
data = infile.read()
lhs, rhs = data.split(":", 1)
ssid1 = ''.join(lhs.split())[:-2]
####
with open('iwlist/sep2.txt', 'r') as infile:
data = infile.read()
lhs, rhs = data.split(":", 1)
ssid2 = ''.join(lhs.split())[:-2]
####
with open('iwlist/sep3.txt', 'r') as infile:
data = infile.read()
lhs, rhs = data.split(":", 1)
ssid3 = ''.join(lhs.split())[:-2]
####
with open('iwlist/sep4.txt', 'r') as infile:
data = infile.read()
lhs, rhs = data.split(":", 1)
ssid4 = ''.join(lhs.split())[:-2]
####
with open('iwlist/sep5.txt', 'r') as infile:
data = infile.read()
lhs, rhs = data.split(":", 1)
ssid5 = ''.join(lhs.split())[:-2]
app = Flask(__name__)
class WifiForm(Form):
password = StringField(validators=[InputRequired()])
@app.route('/', methods=['GET', 'POST'])
def wifi_password1():
form = WifiForm(csrf_enabled=False)
if form.validate_on_submit():
password = form.password.data
with open('wpa_supplicant.conf', 'w') as f:
f.write("ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\nupdate_config=1\n\nnetwork={\n\tssid=\"" + ssid1 + "\"" + "\n\tpsk=\"" + password + "\"" + "\n\tproto=RSN\n\tkey_mgmt=WPA-PSK\n\tpairwise=CCMP\n\tauth_alg=OPEN\n}")
return redirect(url_for('wifi_password1'))
return render_template('main.html',form=form)
#
@app.route('/', methods=['GET', 'POST'])
def wifi_password2():
form = WifiForm(csrf_enabled=False)
if form.validate_on_submit():
password = form.password.data
with open('wpa_supplicant.conf', 'w') as f:
f.write("ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\nupdate_config=1\n\nnetwork={\n\tssid=\"" + ssid2 + "\"" + "\n\tpsk=\"" + password + "\"" + "\n\tproto=RSN\n\tkey_mgmt=WPA-PSK\n\tpairwise=CCMP\n\tauth_alg=OPEN\n}")
return redirect(url_for('wifi_password2'))
return render_template('main.html',form=form)
#
@app.route('/', methods=['GET', 'POST'])
def wifi_password3():
form = WifiForm(csrf_enabled=False)
if form.validate_on_submit():
password = form.password.data
with open('wpa_supplicant.conf', 'w') as f:
f.write("ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\nupdate_config=1\n\nnetwork={\n\tssid=\"" + ssid3 + "\"" + "\n\tpsk=\"" + password + "\"" + "\n\tproto=RSN\n\tkey_mgmt=WPA-PSK\n\tpairwise=CCMP\n\tauth_alg=OPEN\n}")
return redirect(url_for('wifi_password3'))
return render_template('main.html',form=form)
#
@app.route('/', methods=['GET', 'POST'])
def wifi_password4():
form = WifiForm(csrf_enabled=False)
if form.validate_on_submit():
password = form.password.data
with open('wpa_supplicant.conf', 'w') as f:
f.write("ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\nupdate_config=1\n\nnetwork={\n\tssid=\"" + ssid4 + "\"" + "\n\tpsk=\"" + password + "\"" + "\n\tproto=RSN\n\tkey_mgmt=WPA-PSK\n\tpairwise=CCMP\n\tauth_alg=OPEN\n}")
return redirect(url_for('wifi_password4'))
return render_template('main.html',form=form)
#
@app.route('/', methods=['GET', 'POST'])
def wifi_password5():
form = WifiForm(csrf_enabled=False)
if form.validate_on_submit():
password = form.password.data
with open('wpa_supplicant.conf', 'w') as f:
f.write("ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\nupdate_config=1\n\nnetwork={\n\tssid=\"" + ssid5 + "\"" + "\n\tpsk=\"" + password + "\"" + "\n\tproto=RSN\n\tkey_mgmt=WPA-PSK\n\tpairwise=CCMP\n\tauth_alg=OPEN\n}")
return redirect(url_for('wifi_password5'))
return render_template('main.html',form=form)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80, debug=True)
HTML:
<tr><td><h2 class="close"><a href="#one" data-transition="flip">
Quiana
</a>
<tr><td><h2 class="close"><a href="#two" data-transition="flip">
PaintItOrangeDucks
</a>
<tr><td><h2 class="close"><a href="#three" data-transition="flip">
Antimatter
</a>
<tr><td><h2 class="close"><a href="#four" data-transition="flip">
zaragoza1
</a>
<tr><td><h2 class="close"><a href="#five" data-transition="flip">
Palkimawar
</a>
</tbody>
</table>
</div>
</div>
</div>
<div data-role="page" data-dialog="true" id="one">
<div data-role="header">
<h1>
Quiana
</h1>
</div>
<div data-role="main" class="ui-content">
<p>Please enter your Wi-Fi password.
<br><br>
<form method="POST">
{{ form.hidden_tag() }}
{{ form.password }}
<input type="submit" value="CONNECT"/>
</form>
</div>
</div>
</div>
<div data-role="page" data-dialog="true" id="two">
<div data-role="header">
<h1>
PaintItOrangeDucks
</h1>
</div>
<div data-role="main" class="ui-content">
<p>Please enter your Wi-Fi password.
<br><br>
<form method="POST">
{{ form.hidden_tag() }}
{{ form.password }}
<input type="submit" value="CONNECT"/>
</form>
</div>
</div>
</div>
I need to know how to assign each flask form its own name, so when the user submits the form (post) it will write the file with the proper SSID...
I'm sure theres a better way to execute this form! thank you for all your help.