curl:(7)使用 Flask 时无法连接到 127.0.0.1 端口 5000:连接被拒绝

curl:(7)使用 Flask 时无法连接到 127.0.0.1 端口 5000:连接被拒绝

我正在学习这里的 Udacity 课程:https://classroom.udacity.com/courses/ud388/lessons/4628431284/concepts/53174719510923

我正在尝试使用 Flask 运行他们的 Python 程序。我使用以下命令启动了 vagrant:vagrant ssh

然后我尝试通过输入来运行该python文件。 但是当我在浏览器中python api_server.py访问时,我得到:http://0.0.0.0:5000/

Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again. 

然后我尝试输入curl 'http://127.0.0.1:5000'vagrant 但出现错误

vagrant@vagrant-ubuntu-trusty-32:~$ curl 'http://127.0.0.1:5000'
curl: (7) Failed to connect to 127.0.0.1 port 5000: Connection refused

api_server.py 文件如下:

#THIS IS A WEBSERVER FOR DEMONSTRATING THE TYPES OF RESPONSES WE SEE FROM AN API ENDPOINT
from flask import Flask
app = Flask(__name__)

#GET REQUEST

@app.route('/readHello')
def getRequestHello():
    return "Hi, I got your GET Request!"

#POST REQUEST
@app.route('/createHello', methods = ['POST'])
def postRequestHello():
    return "I see you sent a POST message :-)"
#UPDATE REQUEST
@app.route('/updateHello', methods = ['PUT'])
def updateRequestHello():
    return "Sending Hello on an PUT request!"

#DELETE REQUEST
@app.route('/deleteHello', methods = ['DELETE'])
def deleteRequestHello():
    return "Deleting your hard drive.....haha just kidding! I received a     DELETE request!"

if __name__ == '__main__':
    app.debug = True
    app.run(host='0.0.0.0', port=5000)  

答案1

如果您在 Docker 上运行,并且尝试从另一个 docker 控制台进行连接,请确保使用“Docker exec”连接进程以测试服务。我尝试使用“docker run”进行连接,它正在创建另一个 docker 实例,而 curl 却给我 127.0.0.1:5000 的“连接被拒绝”。

相关内容