flask+uwsgi+Nginx

一、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最小运行示例

test.py

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最小运行示例

test.py

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\"
  }"
相关推荐
wno7044 小时前
Nginx禁止蜘蛛程序访问
运维·nginx
Thanks_ks7 小时前
提示词工程实战:用结构化提示词让大模型输出稳定可控
python·大模型·json·openai·提示词工程·deepseek·系统提示词
BK贝壳9 小时前
布尔数组也能做按位取反、移位和乘法:bool-hybrid-array 的位运算在本机跑了一遍
开发语言·数据结构·python·性能优化·numpy
ZhengEnCi9 小时前
前端日志和后端日志怎么记:分级、JSON 格式、按天分文件与保留策略
前端·后端·python
用户03321266636713 小时前
使用 Python 为 PDF 文件添加页眉页脚
python
xurileidian14 小时前
第 17 篇 · 边玩边学Python · for 遍历 enumerate 与 zip
java·前端·python
郝学胜-神的一滴15 小时前
Effective Python 条款45:巧用属性注解平滑升级类属性
运维·开发语言·数据库·python·网络协议·numpy
神一样的老师16 小时前
WS63 BLE 配网实战:用 Python 在 PC 上替代手机完成 WiFi 配网
开发语言·python·智能手机
AI创界者16 小时前
2026 全平台 AI 视频换脸工作流实战:一套方案打通 N 卡、A 卡、I 卡与 Win / Mac
人工智能·python·aigc·音视频
DanCheng-studio17 小时前
计科毕设创新的选题思路
python·毕业设计·毕设