簡體   English   中英

為什么 Flask 公共服務器沒有收到請求?

[英]Why isn't the Flask public server receiving requests?

我正在嘗試在 windows 10 機器上設置服務器,使用 Python 和 Flask,但它不響應外部請求。

這是我的server.py文件:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def index():
    return "Hi there"

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)

運行時,它說:

 * Running on all addresses (0.0.0.0)
   WARNING: This is a development server. Do not use it in a production deployment.
 * Running on http://127.0.0.1:5000
 * Running on http://195.XX.XXX.XXX:5000 (Press CTRL+C to quit)

事實上,如果我嘗試使用127.0.0.1:5000195.XX.XXX.XXX:5000台機器訪問它,它可以正常工作。

但是,當嘗試從另一台機器訪問它時(如果這可能是個問題,請使用 Chrome),它只是無限期地加載,然后說沒有收到數據, ERR_EMPTY_RESPONSE

這有什么問題? 我已經按照文檔中的步驟進行操作,所以我不知道可能出了什么問題。

我還在 windows 10 機器上完全禁用了防火牆。

您可能只需要打開端口, 也可能希望使用網絡服務器路由 HTTP 請求,您可以使用 Apache、IIS 或 NGINX。

按照此說明在 Windows 10 中打開防火牆端口

You can manually permit a program to access the internet by opening a firewall port. You will need to know what port it uses and the protocol to make this work.

Navigate to Control Panel, System and Security and Windows Firewall.
Select Advanced settings and highlight Inbound Rules in the left pane.
Right click Inbound Rules and select New Rule.
Add the port you need to open and click Next.
Add the protocol (TCP or UDP) and the port number 5000 into the next window and click Next.
Select Allow the connection in the next window and hit Next.
Select the network type as you see fit and click Next.
Name the rule something meaningful and click Finish.
You have now opened a firewall port in Windows 10!

你的跑步方式很重要。 如果是通過

python server.py

然后app.run()將執行,服務器將綁定到 0.0.0.0

但是,如果您通過某種變體運行

FLASK_APP=server.py flask run

然后app.run()不會執行,服務器會綁定到 127.0.0.1

在這種情況下,添加--host=0.0.0.0應該可以解決問題(除非您遇到防火牆問題)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM