Here is a simple Flask setup.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello, World!'
host = '123.123.123.123'
app.run(host=host)
What can I do to check that the host
IP address is valid before executing app.run(host)
, in order to avoid the Can't assign requested address
error?
Are you trying to bind to a particular interface? If not, '0.0.0.0' is the way to go. If you are trying to bind to a particular interface you can check that interfaces existence before hand, using things like ifconfig
on Linux systems. Or brute force it, by trying to bind to it, and see if it succeeds.
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('123.123.123.123', 8080))
If this succeeds, you know you have an interface with that IP addr.