一、flask最小运行示例
python
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello, Flask!\n"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7777, debug=True)
运行之后,另外开启终端,执行以下命令,可顺利看到结果
bash
curl http://0.0.0.0:7777
二、uwsgi+flask最小运行示例
python
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "hello flask+uwsgi\n"
uwsgi.ini
bash
[uwsgi]
http = :7777
chdir = .
wsgi-file = test.py
callable = app
master = true
processes = 1
threads = 1
vacuum = true
die-on-term = true
logto = uwsgi.log
以下命令开启uwsgi服务
bash
uwsgi --ini uwsgi.ini
运行之后,另外开启终端,执行http请求
bash
curl http://0.0.0.0:7777/
三、复杂案例示例
python
import io
import base64
from PIL import Image
from flask import Flask, jsonify, request
app = Flask(__name__)
@app.route("/run", methods=['POST'])
def process():
if request.form:
parameters = {k: request.form.get(k).strip() for k in request.form}
else:
parameters = {k: v for k, v in request.json.items()}
image = parameters.get('image', '')
if image:
try:
# 检查是否是base64格式
if image.startswith('data:image'):
# 移除data:image/xxx;base64,前缀
img_data = image.split(',')[1] if ',' in image else image
img_bytes = base64.b64decode(img_data)
elif image.startswith(('http://', 'https://')):
img_bytes = None
else:
# 直接当作base64处理
img_bytes = base64.b64decode(image)
if img_bytes:
img = Image.open(io.BytesIO(img_bytes)).convert('RGB')
width, height = img.size
return jsonify({"result": {"image_w": width,"image_h": height}, "error_code": 0})
except Exception as e:
print('处理图片获取宽高失败:')
else:
print('image参数为空')
return jsonify({"result": {}, "error_code": 1})
@app.route("/hello")
def hello():
return "Hello\n"
bash
curl -X POST http://0.0.0.0:7788/run \
-H "Content-Type: application/json" \
-d "{
\"image\": \"data:image/jpeg;base64,$IMAGE_BASE64\"
}"
一、flask最小运行示例
python
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello, Flask!\n"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7777, debug=True)
运行之后,另外开启终端,执行以下命令,可顺利看到结果
bash
curl http://0.0.0.0:7777
二、uwsgi+flask最小运行示例
python
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "hello flask+uwsgi\n"
uwsgi.ini
bash
[uwsgi]
http = :7777
chdir = .
wsgi-file = test.py
callable = app
master = true
processes = 1
threads = 1
vacuum = true
die-on-term = true
logto = uwsgi.log
以下命令开启uwsgi服务
bash
uwsgi --ini uwsgi.ini
运行之后,另外开启终端,执行http请求
bash
curl http://0.0.0.0:7777/
三、复杂案例示例
python
import io
import base64
from PIL import Image
from flask import Flask, jsonify, request
app = Flask(__name__)
@app.route("/run", methods=['POST'])
def process():
if request.form:
parameters = {k: request.form.get(k).strip() for k in request.form}
else:
parameters = {k: v for k, v in request.json.items()}
image = parameters.get('image', '')
if image:
try:
# 检查是否是base64格式
if image.startswith('data:image'):
# 移除data:image/xxx;base64,前缀
img_data = image.split(',')[1] if ',' in image else image
img_bytes = base64.b64decode(img_data)
elif image.startswith(('http://', 'https://')):
img_bytes = None
else:
# 直接当作base64处理
img_bytes = base64.b64decode(image)
if img_bytes:
img = Image.open(io.BytesIO(img_bytes)).convert('RGB')
width, height = img.size
return jsonify({"result": {"image_w": width,"image_h": height}, "error_code": 0})
except Exception as e:
print('处理图片获取宽高失败:')
else:
print('image参数为空')
return jsonify({"result": {}, "error_code": 1})
@app.route("/hello")
def hello():
return "Hello\n"
bash
curl -X POST http://0.0.0.0:7788/run \
-H "Content-Type: application/json" \
-d "{
\"image\": \"data:image/jpeg;base64,$IMAGE_BASE64\"
}"

