python 开启https服务

import json
from flask import Flask, Response, request
import os

app = Flask(__name__)

# 设置 SSL 证书路径
ssl_cert_path = os.path.join(os.path.dirname(__file__), 'certs', 'self.crt')
ssl_key_path = os.path.join(os.path.dirname(__file__), 'certs', 'self.key')

@app.route('/aaa', methods=['POST'])
def aaa():
    try:
        data = request.get_json()
        if data.get("data"):
            success = {"success":True,"data":"ok"}
        else:
            success = {"success":False,"error":"参数错误"}

        json_str = json.dumps(success, ensure_ascii=False)  
        response = Response(json_str, content_type="application/json; charset=utf-8")
        return response
    except Exception as e:
        error_response = {"success": False, "error": f"错误 {str(e)}"}
        return Response(json.dumps(error_response, ensure_ascii=False), content_type="application/json; charset=utf-8")

# **启动 Flask 应用,启用 HTTPS**
if __name__ == '__main__':
    app.run(host='0.0.0.0',port=54321,debug=True, ssl_context=(ssl_cert_path, ssl_key_path))  # 启用 HTTPS

你可能感兴趣的:(python,python,https,开发语言)