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\"
  }"
相关推荐
cch891816 分钟前
Python主流框架全解析
开发语言·python
海的透彻27 分钟前
nginx启动进程对文件的权限掌控
运维·chrome·nginx
sg_knight29 分钟前
设计模式实战:状态模式(State)
python·ui·设计模式·状态模式·state
好运的阿财37 分钟前
process 工具与子agent管理机制详解
网络·人工智能·python·程序人生·ai编程
张張4081 小时前
(域格)环境搭建和编译
c语言·开发语言·python·ai
weixin_423533991 小时前
【Windows11离线安装anaconda、python、vscode】
开发语言·vscode·python
Ricky111zzz1 小时前
leetcode学python记录1
python·算法·leetcode·职场和发展
小白学大数据1 小时前
Selenium+Python 爬虫:动态加载头条问答爬取
爬虫·python·selenium
Hui Baby2 小时前
springboot读取配置文件
后端·python·flask
阿Y加油吧2 小时前
回溯法经典难题:N 皇后问题 深度解析 + 二分查找入门(搜索插入位置)
开发语言·python