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\"
  }"
相关推荐
做怪小疯子1 天前
华为笔试0429
python·numpy
Warson_L1 天前
Dictionary
python
寒山李白1 天前
解决 python-docx 生成的 Word 文档打开时弹出“无法读取内容“警告
python·word·wps·文档·docx·qoder
2401_832365521 天前
JavaScript中rest参数(...args)取代arguments的优势
jvm·数据库·python
Sirius.z1 天前
第J3周:DenseNet121算法详解
python
2301_779622411 天前
Go语言怎么用信号量控制并发_Go语言semaphore信号量教程【入门】
jvm·数据库·python
2301_766283441 天前
c++如何将控制台输出保存到文件_cout重定向到txt【详解】
jvm·数据库·python
Jinkxs1 天前
LoadBalancer- 主流负载均衡工具盘点:Nginx / Haproxy / Keepalived 基础介绍
运维·nginx·负载均衡
小康小小涵1 天前
基于ESP32S3实现无人机RID模块底层源码编译
linux·开发语言·python
lzjava20241 天前
Python的函数
开发语言·python