Flask listens only on 127.0.0.1 ignoring host parameter

11,590

The port goes in it's own parameter:

app.run(
    host="0.0.0.0",
    port=5000
)
Share:
11,590
Valentin H
Author by

Valentin H

Updated on July 25, 2022

Comments

  • Valentin H
    Valentin H almost 2 years

    I'm using Flask on Windows 7. Flask and related versions are below:

    Flask==0.10.1
    Werkzeug==0.9.3
    

    Accessing the app from the same computer is OK using http://127.0.0.1:5000 However from another computer in LAN the access fails: http://192.168.101.103:5000

    I start the app with these parameters:

    #app.py
    if __name__ == '__main__':
        app.run( host='0.0.0.0:5000')
    

    One thing I don't understand is, when I start netcat on the same computer, where Flask is currently listening on the same port, it works, and netcat is even accessible from another computer:

    >c:\Python27\python manage.py runserver
    >
     * Running on http://127.0.0.1:5000/
     * Restarting with reloader
    

    works ...

    >nc -l -p 5000
    

    works on the same computer, same port ???

    GET / HTTP/1.1
    Accept: text/html, application/xhtml+xml, */*
    Accept-Language: de-DE
    User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0;
    
    Accept-Encoding: gzip, deflate
    Host: 192.168.101.103:5000
    DNT: 1
    Connection: Keep-Alive
    

    even accepting connection from another computer ???

    So beside the obvious question, how to get Flask serving for LAN, I'm curious, how could two processes of one machine listen on the same port?

    Thank you!

  • Valentin H
    Valentin H almost 10 years
    Looks like the statement was ignored. I disabled if __name__ == '__main__': and got the error on app.run( host="0.0.0.0:5000") Now it works, but have strange start/stop behavior. Sometimes on restart it Flask though binds to 127.0.0.1. I think I have to go for nginx as the proxy server for the Flask for serving in LAN
  • Rachel Sanders
    Rachel Sanders almost 10 years
    Don't use the built-in server for anything but development work. It's not built for it. In production, use gunicorn or another container.