Flask access from different devices

谁说胖子不能爱 提交于 2021-01-28 18:22:48

问题


Is there a way to generate a specific IP address or make a specific site of flask http://127.0.0.1:5000/ site which runs locally, to access the web-app made using flask from different device as by default it runs locally and under http://127.0.0.1:5000/ but,i want to access it from different devices.If,there's a way please help


回答1:


refer first to this doc (section Externally Visible Server) on how to expose your local Flask app to make it accessible from trusted devices in your network for testing purposes.

$(venv) flask run --host=0.0.0.0

or in your app.py

from flask import Flask
[..]
app = Flask(__name__)
[..]
if __name__ == "__main__":
   app.run(host="0.0.0.0", port=5000, debug=True)

and then :

$(venv) python app.py

but if it happens and you got this error dial tcp 0.0.0.0:5000: connect: connection refused then try to use the local ip address (192.168.x.y instead of 0.0.0.0) of the machine hosting your Flask app. you may find this thread usefull




回答2:


  • Instead of localhost (127.0.0.1), you need to type your router's public IP. If you do not already know it, you can retrieve it by typing ipconfig (for Windows) or ifconfig (for Linux) in the command line.
  • You also need to disable your firewall or add an inbound rule for the port used by your server (e.g. 5000).



回答3:


You can access your server on devices in the same Network / Wifi with your private IP address and the port. On Mac OS option + click on Wifi in your Taskbar.

On linux: > ifconfig



来源:https://stackoverflow.com/questions/62499969/flask-access-from-different-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!