1 ##############################################################################
\r
2 # Copyright (c) 2018 HUAWEI TECHNOLOGIES CO.,LTD and others.
\r
4 # All rights reserved. This program and the accompanying materials
\r
5 # are made available under the terms of the Apache License, Version 2.0
\r
6 # which accompanies this distribution, and is available at
\r
7 # http://www.apache.org/licenses/LICENSE-2.0
\r
8 ##############################################################################
\r
10 from flask import Flask
\r
11 from flask_cors import CORS
\r
12 from flask import request
\r
13 from flask import jsonify
\r
16 from random import randint
\r
18 app = Flask(__name__)
\r
22 @app.route("/greet")
\r
27 @app.route("/answer", methods=["POST"])
\r
29 app.logger.debug(request.form)
\r
30 app.logger.debug(request.data)
\r
31 if jsonify(request.form) != {} and 'ping' in request.form:
\r
32 return "answer: ping is: \"" + request.form['ping'] + "\" end."
\r
33 elif request.data != "":
\r
34 requestDict = json.loads(request.data)
\r
35 if 'ping' in requestDict:
\r
36 return "answer: the ping is: \"" + requestDict['ping'] + "\" end."
\r
38 return "answer ping is null"
\r
41 @app.route("/answer2", methods=["POST"])
\r
47 def sleepFiveSeconds():
\r
49 return "five: receive the request."
\r
53 def sleepTenSeconds():
\r
55 return "ten: receive the request."
\r
58 @app.route("/switch")
\r
60 value = randint(0, 10)
\r
62 return jsonify({'code': 200, 'result': 'A'})
\r
64 return jsonify({'code': 200, 'result': 'B'})
\r
67 @app.route("/switch_2")
\r
68 def switchValue_2():
\r
69 value = randint(0, 10)
\r
71 return jsonify({'code': 200, 'result': 'C'})
\r
73 return jsonify({'code': 200, 'result': 'D'})
\r
76 if __name__ == "__main__":
\r
77 app.run(host='0.0.0.0', port=5312, debug=True)
\r