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\"
  }"
相关推荐
聪明蛋子哟6 小时前
Stagehand v3多语言SDK:Python/Go/Rust/Java下的浏览器自动化统一方案
python·golang·rust
Jimmy_jimi6 小时前
利用nginx代理完成url重定位
运维·nginx
今天AI了吗7 小时前
Python 基础语法(一):常量、变量、输入输出与运算符
开发语言·数据库·人工智能·python·sql·深度学习·机器学习
卷无止境7 小时前
Windows 上丝滑开发 Python,并稳定构建 Docker 镜像
后端·python·docker
TELL5217 小时前
selenium webdriver 第二次初始化的异常
开发语言·python
Csvn8 小时前
🐍 Day 4: Python 控制流 — 条件、循环与推导式的艺术
后端·python
大鹏说大话10 小时前
从爬虫到决策引擎:大数据下自媒体如何用Python挖掘用户痛点
开发语言·爬虫·python
Niuguangshuo10 小时前
silero-vad:超轻量级开源 VAD 实践指南
开发语言·python
2501_9446761611 小时前
揭秘!WORDTIP公司靠不靠谱?小白必看
大数据·人工智能·python
Minner-Scrapy11 小时前
Scrapy 2.17 源码解析:Scheduler 调度器与磁盘/内存双队列
java·爬虫·python·scrapy·网络爬虫·twisted