![curl:(7)使用 Flask 时无法连接到 127.0.0.1 端口 5000:连接被拒绝](https://linux22.com/image/1088158/curl%EF%BC%9A%EF%BC%887%EF%BC%89%E4%BD%BF%E7%94%A8%20Flask%20%E6%97%B6%E6%97%A0%E6%B3%95%E8%BF%9E%E6%8E%A5%E5%88%B0%20127.0.0.1%20%E7%AB%AF%E5%8F%A3%205000%EF%BC%9A%E8%BF%9E%E6%8E%A5%E8%A2%AB%E6%8B%92%E7%BB%9D.png)
我正在学习这里的 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 的“连接被拒绝”。