Imran Teli
Imran Teli

Reputation: 53

Flask app crashes "Restarting with reloader....socket.error: [Errno 98] Address already in use

In my flask app I import a file, which has python variables. I then pass this variable to html template and show it on the browser using jinja2 templating.

What I am doing here is I redirect user to a url which renders a html file and passes this python variable to jinja2 template. I update this file and change variables value time to time using a script.

When I update my module file and change variables value, My flask app crashes. Please let me know what I am doing wrong, I am new to web development. Also I want to know if their is anyway we can import python variable in Jinja2 template.

* Detected change in '/root/Tas/modules/steps.py', reloading
* Restarting with reloader
Traceback (most recent call last):
File "routes.py", line 45, in <module>
app.run(host='0.0.0.0', debug=True,use_reloader=True)
 File "/root/Tas/venv/lib/python2.6/site-packages/flask/app.py", line 772, in run
run_simple(host, port, self, **options)
File "/root/Tas/venv/lib/python2.6/site-packages/werkzeug/serving.py", line 706, in   run_simple
test_socket.bind((hostname, port))
File "<string>", line 1, in bind
socket.error: [Errno 98] Address already in use

My code:

from flask import Flask, render_template, request, flash, redirect, url_for
from forms import UpgradeForm
import subprocess
import os
import sys
sys.path.append('/root/Tas/modules/')
import time
import reko
import tibbr_path
import steps

app = Flask(__name__)
app.secret_key = 'development key'

@app.route('/')
def home():
    return render_template('home.html')


@app.route('/upgrade', methods=['GET', 'POST'])
def upgrade():
    form = UpgradeForm(request.form)
    if request.method == 'POST':
            if form.validate() == False:
                    flash('All Fields are required')
                    return render_template('upgrade.html', form=form)
            else:
                    fo = open("/root/Tas/modules/reko.py", "wb")
                    print form.username.data
                            fo.write("username='%s'\npassword='%s'\npack_pack='%s'\nup_server='%s'\n" % (form.username.data, form.password.data, form.pack_pack.data, form.up_server.data))
                    fo.close()
                    return render_template('validate.html', form=form)
    elif request.method == 'GET':
            return render_template('upgrade.html', form=form)

@app.route('/exec_SNupgrade', methods=['GET'])
def exec_SNupgrade():
     os.system("/root/Tas/scripts/int_SNupgrade.py &")
     return redirect(url_for('display_step'))
     )
@app.route('/display_step', methods=['GET'])
def display_step():
    return render_template('display_step.html', up_server=reko.up_server, step1=steps.step1 , step2=steps.step2, step3=steps.step3, step4=steps.step4)
if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True,use_reloader=True)

Upvotes: 3

Views: 5677

Answers (2)

Ashoka Lella
Ashoka Lella

Reputation: 6729

If the address is already in use change the default port

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True, port=12345, use_reloader=True)

you can now access the page through http://localhost:12345

Upvotes: 3

Alex P
Alex P

Reputation: 6072

'Address already in use' means you've got another server running on that computer that's bound to the same port. Turn it off, and you should be good to go.

Upvotes: 0

Related Questions