flask server

HelloWorld

1
2
3
4
5
6
7
8
9
10
11
from flask import Flask
from flask import Request

app = Flask(__name__)

@app.route('/hello')
def hello():
return 'Hello World'

if __name__=='__main__':
app.run(host='0.0.0.0',port=1234)

简单函数回显传入的参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import flask as falsk

app =falsk.Flask(__name__)

@app.route("/predict",methods=["GET","POST"])
def predict():
data = {"success":False}
# 获取请求参数
params=flask.request.json
if(params==None):
params=flask.request.json
if(params==None):
params=flask.request.args
if(params!=None):
data["response"]=params.get("msg")
data["success"]=True
return flask.jsonify(data)

app.run(host='0.0.0.0')

需要ec2,我用windows本地运行,本地打开localhost:5000,报Internal Server Error

而实际上,Flask启动的服务环境不够稳定也无法承受大量请求的并发,实际部署过程中配合需要WSGI(Web Server Gateway Interface)使用。

1
pip install uwsgi