此宁静的客户端调用在“ test_rest.py”中定义的函数“ return_result”。
test_rest.py:
print("how may times this is called?")
def return_result(sentence):
print(sentence)
我以这种方式提出了请求:
http://127.0.0.1:4000/sentence=hello
然后是我的rest.py
from flask import Flask, make_response
from flask_restful import Resource, Api
from test_rest import return_result
app = Flask(__name__)
api = Api(app)
class HelloWorld(Resource):
def get(self, sentence):
print("sentence:{}".format(sentence))
c = return_result(sentence)
print("result:{}".format(c))
r = make_response(str(c))
r.mimetype = 'application/json'
return r
api.add_resource(HelloWorld, '/<string:sentence>')
if __name__ == '__main__':
app.run(debug=True, host='127.0.0.1', port=5000)
控制台上的输出消息是:
how may times this is called?
* Serving Flask app "rest" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
how may times this is called?
* Debugger is active!
* Debugger PIN: 289-783-459
如您所见,当我在浏览器中发出请求时,“调用了多少次”生成了两次?
这是为什么,以及如何纠正此宁静的服务功能?原因是当前的静态服务方式在我的程序中引起了问题。