Python 无法连接到 Flask Web 服务,连接被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30554702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:38:28  来源:igfitidea点击:

Can't connect to Flask web service, connection refused

pythonflask

提问by Yashar

I'm trying to run a simple web server on a Raspberry Pi with Flask. When I run my Flask app, it says:

我正在尝试使用 Flask 在 Raspberry Pi 上运行一个简单的 Web 服务器。当我运行 Flask 应用程序时,它说:

running on http://127.0.0.1:5000/

http://127.0.0.1:5000/ 上运行

But when I enter this address on my laptop's in Chrome, I get

但是当我在 Chrome 的笔记本电脑上输入这个地址时,我得到

ERR_CONNECTION_REFUSED

ERR_CONNECTION_REFUSED

I can open 127.0.0.1:5000 on the Raspberry Pi's browser. What do I need to do to connect from another computer?

我可以在树莓派的浏览器上打开 127.0.0.1:5000。我需要做什么才能从另一台计算机连接?

采纳答案by salmanwahed

Run your app like this:

像这样运行你的应用程序:

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

It will make the server externally visible. If the IP address of the machine is 192.168.X.Xthen, from the same network you can access it in 5000 port. Like, http://192.168.X.X:5000

它将使服务器对外可见。如果机器的IP地址是192.168.X.X然后,从同一个网络你可以在5000端口访问它。喜欢,http://192.168.XX:5000

回答by Hitesh Dharamdasani

You will have to run the development server such that it listens to requests on all interfaces and not just the local one

您必须运行开发服务器,以便它侦听所有接口上的请求,而不仅仅是本地接口

Ask Flask to listen on 0.0.0.0:PORT_NUMBER

要求 Flask 收听 0.0.0.0:PORT_NUMBER

or any other port you may choose

或您可以选择的任何其他端口

回答by Stiffy2000

when you are running the server via flask runchange it to flask run --host=0.0.0.0to connect, find the IPV4 address of the server that your script is running on. On the same network, go to http://[IPV4 address]:5000

当您通过flask run将其更改flask run --host=0.0.0.0为连接来运行服务器时,找到运行脚本的服务器的 IPV4 地址。在同一网络上,转到http://[IPV4 address]:5000

回答by Artem Zaytsev

A reason could also be in firewallrefusing incoming connections on port 5000. Try:

一个原因也可能是防火墙拒绝端口 5000 上的传入连接。尝试:

sudo ufw allow 5000

回答by Muhammad Usman

app.run(host='0.0.0.0',port=5000)

if you run your app in this way then your server will be visible externally. Steps by Setp:

如果您以这种方式运行您的应用程序,那么您的服务器将在外部可见。Setp 的步骤:

  1. Run your app by using the following command

    app.run(host='0.0.0.0',port=5000)

  2. Go to the window cmd . Type ipconfigand get the get the IPV4 adress suppose your IPV4 address is 192.168.X.X

  3. Go to the mobile browser and type the 192.168.X.X:5000

  1. 使用以下命令运行您的应用程序

    app.run(host='0.0.0.0',port=5000)

  2. 转到窗口 cmd 。输入ipconfig并获取 IPV4 地址,假设您的 IPV4 地址是 192.168.XX

  3. 转到移动浏览器并输入 192.168.XX:5000

回答by mouserat

If you have "debug = True" inside your app.run(), then it will not be visible to other machines either. Specify host and port inside app.run() without the "debug = True".

如果您的 app.run() 中有“debug = True”,那么其他机器也看不到它。在 app.run() 中指定主机和端口,不带“debug = True”。