简体   繁体   中英

Why won't my flask server bind my ip address?

I have tried running from localhost, 0.0.0.0, and from my ipv4 address. When I used localhost, I could not access my server from another computer. When I used 0.0.0.0, I had the same problem, and when I used my public ipv4 address it threw an error.

My code:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
        return "Hello World!"

if __name__ == "__main__":
    app.run(host="[my ip address]")

my error:

 * Serving Flask app "hello" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
Traceback (most recent call last):
  File "hello.py", line 14, in <module>
    app.run(host="[my ip address]")
  File "/home/boomerhackr/.local/lib/python3.6/site-packages/flask/app.py", line 990, in run
    run_simple(host, port, self, **options)
  File "/home/boomerhackr/.local/lib/python3.6/site-packages/werkzeug/serving.py", line 1052, in run_simple
    inner()
  File "/home/boomerhackr/.local/lib/python3.6/site-packages/werkzeug/serving.py", line 1005, in inner
    fd=fd,
  File "/home/boomerhackr/.local/lib/python3.6/site-packages/werkzeug/serving.py", line 848, in make_server
    host, port, app, request_handler, passthrough_errors, ssl_context, fd=fd
  File "/home/boomerhackr/.local/lib/python3.6/site-packages/werkzeug/serving.py", line 740, in __init__
    HTTPServer.__init__(self, server_address, handler)
  File "/usr/lib/python3.6/socketserver.py", line 456, in __init__
    self.server_bind()
  File "/usr/lib/python3.6/http/server.py", line 136, in server_bind
    socketserver.TCPServer.server_bind(self)
  File "/usr/lib/python3.6/socketserver.py", line 470, in server_bind
    self.socket.bind(self.server_address)
OSError: [Errno 99] Cannot assign requested address

To find my ip address I used the bash command curl ifconfig.me .

my OS:

NAME="Ubuntu"
VERSION="18.04.4 LTS (Bionic Beaver)"

Is this a problem with Ubuntu, or with my code?

The best way to tackle this would be to have the following:

app.run(host='0.0.0.0', port='8000') where port could be changed as per convenience.

When you use app.run without any parameters, it defaults to host as 127.0.0.1 it binds to your local system. In this state, your application cannot be connected via any other system either through LAN or WAN (public address)

When you run it using 0.0.0.0 it's accessible both via LAN as well as WAN. To get your LAN Address in Ubuntu, you could simply run hostname -I and for WAN Address as mentioned, you could use curl ifconfig.me . So, you can access your application from another system on the same LAN by using LAN_IP:PORT

Now, most of the ISP's do not provide a static IP and you cannot expose your WAN IP on the internet. However, when you run the application using 0.0.0.0 on a VPS you could directly use the server's WAN IP and connect.

For your reference, the difference between 127.0.0.1 and 0.0.0.0 can be found here

"curl ifconfig.me" give you your public IP address. Sometimes it is assigned to a router and in several cases the LAN use NAT. That means that you computer could has a private IP address. Did you verify the addresses assigned on your interfaces with the command ifconfig ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM