方法一:
python -m SimpleHTTPServer
方法二:
新建文件 server.py ,内容如下
from flask import Flask
from flask import request, jsonify, send_from_directory
app = Flask(__name__, static_url_path='')
# class InvalidUsage(Exception):
# status_code = 400
# def __init__(self, message, status_code=None, payload=None):
# Exception.__init__(self)
# self.message = message
# if status_code is not None:
# self.status_code = status_code
# self.payload = payload
# def to_dict(self):
# rv = dict(self.payload or ())
# rv['message'] = self.message
# return rv
# @app.errorhandler(InvalidUsage)
# def handle_invalid_usage(error):
# response = jsonify(error.to_dict())
# response.status_code = error.status_code
# return response
@app.route("/hello", methods=['GET'])
def hello():
return "Hello, world!"
@app.route('/img/<path:path>')
def send_image(path):
"static files"
return send_from_directory("./img", path)
@app.route("/add", methods=['GET', 'POST'])
def add():
numA = request.values.get('numA')
numB = request.values.get('numB')
if numA is None:
raise InvalidUsage('parameter "numA" is missing', status_code=410)
if numB is None:
raise InvalidUsage('parameter "numB" is missing', status_code=410)
result = int(numA) + int(numB)
out = {}
out['numResult'] = result
return jsonify(out)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)
执行命令 python server.py