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\"
  }"
相关推荐
Biehmltym2 小时前
【AI】02实现AI Agent全栈:十分钟,跑通Python调用 Gemini(大模型)的小型Web项目
人工智能·windows·python
山海青风2 小时前
人工智能基础与应用 - 数据处理、建模与预测流程 1 : 了解人工智能
人工智能·python
技术工小李2 小时前
定制邀请函,助力陕西省第五届食管胃静脉曲张及其伴发疾病研讨会圆满落幕
python
淡笑沐白2 小时前
Nginx 详细教程
运维·nginx
AAA简单玩转程序设计2 小时前
Python 基础再升级!5 个超实用小技巧,写代码快人一步
python
dagouaofei2 小时前
2026 年工作计划汇报 PPT:多种 AI 方案对比评估
人工智能·python·powerpoint
唐叔在学习2 小时前
用python实现类AI自动执行终端指令
后端·python·自动化运维
前端程序猿之路2 小时前
AI大模型应用开发之容器化部署
人工智能·python·语言模型·云原生·eureka·ai编程·改行学it
创新技术阁2 小时前
CryptoAiAdmin项目数据库表自动创建和初始化
后端·python·fastapi