當前位置: 首頁>>技術教程>>正文


networking – 如何允許遠程連接到 Flask?

,

問題描述

在係統內部,在虛擬機上運行,​​我可以在 127.0.0.1:5000 訪問正在運行的服務器。

雖然 vm 的 ‘remote’ 地址是 192.168.56.101(ping 和 ssh 工作正常),但我無法從虛擬機或本地訪問具有 192.168.50.101:5000 的服務器。

我猜有些東西阻止了遠程連接。

這是/etc/network/interfaces:

auto eth1
iface eth1 inet static
address 192.168.56.101
netmask 255.255.255.0

ufw 處於非活動狀態。

我該如何解決這個問題?

最佳回答

首先 – 通過檢查以下輸出,確保您的 HTTP 服務器正在偵聽 192.168.50.101:5000 或任何地方( 0.0.0.0:5000 ):

netstat -tupln | grep ':5000'

如果不是,請谘詢 Flask’s documentation 以綁定到 localhost 以外的地址。

如果是,請使用 iptables 允許流量:

iptables -I INPUT -p tcp --dport 5000 -j ACCEPT

來自 Flask 的文檔:

\\n

Externally Visible Server If you run the server you will notice that\\n the server is only accessible from your own computer, not from any\\n other in the network. This is the default because in debugging mode a\\n user of the application can execute arbitrary Python code on your\\n computer.

\\n

If you have debug disabled or trust the users on your network, you can\\n make the server publicly available simply by changing the call of the\\n run() method to look like this:

\\n

app.run(host='0.0.0.0')\\n

\\n

次佳回答

最好的方法

flask run --host=0.0.0.0

第三種回答

我剛遇到同樣的問題。為了解決它,我更新了運行應用程序的方式:

 app.run(debug=True,host='0.0.0.0')

使用 host=0.0.0.0 讓我可以通過本地網絡訪問我的應用程序。

參考資料

本文由Ubuntu問答整理, 博文地址: https://ubuntuqa.com/zh-tw/article/12903.html,未經允許,請勿轉載。