简体   繁体   English

我无法从其他设备连接到 Flask 服务器(=不是来自本地主机)

[英]I can't connect to a Flask server from other devices(=not from localhost)

My goal is to build a RESTful API communication, between a python server and an android (java) client.我的目标是在 python 服务器和 android (java) 客户端之间构建 RESTful API 通信。

I want to start with a client using the URL in chrome, and then with an android-java client.我想从使用 chrome 中的 URL 的客户端开始,然后使用 android-java 客户端。

I have a very simple Flask server:我有一个非常简单的 Flask 服务器:

from flask import Flask
app = Flask(__name__)

@app.route("/RESTfulExample/json/product/get")
def hello():
    return "Hello !!"

if __name__ == "__main__":
    app.run()

When this is localhost(ip=127.0.0.1) the communication between the server and the client works well .当这是 localhost(ip=127.0.0.1) 时,服务器和客户端之间的通信运行良好

The problem is when I run the client from a different device (I tried with a phone and another computer):问题是当我从不同的设备运行客户端时(我用手机和另一台电脑试过):

from flask import Flask
app = Flask(__name__)

@app.route("/RESTfulExample/json/product/get")
def hello():
    return "Hello !!"

if __name__ == "__main__":
    #app.run()
    app.run(host='10.0.0.54', threaded=True)

When I run the server and enter this url ( http://10.0.0.54:5000//RESTfulExample/json/product/get ) in the chrome URL, After 2-3 minutes it returns an ERR_CONNECTION_TIMED_OUT error, or an ERR_ADDRESS_UNREACHABLE error.当我运行服务器并在 Chrome URL 中输入此 url ( http://10.0.0.54:5000//RESTfulExample/json/product/get ) 时,2-3 分钟后它返回 ERR_CONNECTION_TIMED_OUT 错误,或 ERR_ADDRESS_UNREACHABLE 错误。

I don't have a clue about a solution for this simple problem.我对这个简单问题的解决方案一无所知。 I will glad if someone here will help me with it.如果这里有人会帮助我,我会很高兴。

Thanks in advance !!提前致谢!!

Try this试试这个

  • app.run(host='0.0.0.0', debug=True) app.run(host='0.0.0.0', debug=True)
  • Allow TCP traffic on port 5000允许端口 5000 上的 TCP 流量
  • Turn off firewall关闭防火墙

Please set the host in the python script to请将python脚本中的主机设置为

host='0.0.0.0'

With that the Server will listen on all available network Interfaces.这样,服务器将侦听所有可用的网络接口。

You also have to assign your port (also in python):您还必须分配您的端口(也在 python 中):

port=5000

And take care, that your firewall is not blocking requests.并注意,您的防火墙不会阻止请求。 To change the behavior of your firewall you can use (on windows) the integrated firewall tool.要更改防火墙的行为,您可以使用(在 Windows 上)集成防火墙工具。 There you can also easily enable TCP traffic for port 5000 in your local network.在那里,您还可以轻松地为本地网络中的端口 5000 启用TCP流量。

Sometimes it also happens that you have to enable peer to peer traffic in your local router.有时,您还必须在本地路由器中启用对等通信。

Ps: you also have two slashes in your link Ps:您的链接中也有两个斜杠

One way to access flask app from another computer is to get the host computer's IP and run it in another computer.从另一台计算机访问flask应用程序的一种方法是获取主机的IP并在另一台计算机上运行它。

In Windows, go to cmd and type ipconfig and look for your computer ipv4 address Eg, 192.168.0.. In Linux, use ifconfig .在 Windows 中,转到cmd并键入ipconfig并查找您的计算机 ipv4 地址 例如, 192.168.0..在 Linux 中,使用ifconfig

Then, in another machine,然后,在另一台机器上,

http://<host_ipaddress>:<port>/<endpoint>

Eg, http://10.0.0.4:5000/sample例如, http://10.0.0.4:5000/sample

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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