Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check if the IP address for the Flask server is valid?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading