首页 > 解决方案 > cURL 请求拒绝连接 Flask 服务器

问题描述

我有一个简单的 Flask 服务器:

from flask import Flask  
app = Flask(__name__)

@app.route('/')
def hello_world():  
    return 'Hello World!'

当我$ curl http://127.0.0.1:5000/在命令行上运行时,我得到了错误

curl: (7) Failed to connect to 127.0.0.1 port 5000: Connection refused

问题是什么?

标签: pythoncurlflaskget

解决方案


If the code snippet above is exactly the same to your code that you are trying to run then you are missing app.run() statement: Also You can specify any port number with port=<port_number> parameter in app.run()

Try this:

from flask import Flask  
app = Flask(__name__)

@app.route('/')
def hello_world():  
    return 'Hello World!'

if __name__ == '__main__':
    app.run(debug=True)

and then run this script


推荐阅读