python杂项代码

from flask import Flask, escape, url_for,request,render_template,send_from_directory
import os.path
import json
import pprint
from flask_cors import CORS
from werkzeug import secure_filename
app = Flask(__name__)
CORS(app, supports_credentials=True)

#打开主页
@app.route("/", methods=['GET', 'POST'])
def myweb():
    return render_template("GW.html")

if __name__ == '__main__':
    app.run(debug=True)

1、ajax发送json+cookie
html代码

    

JavaScript代码

    function setCookie(name,value){
        document.cookie=name+'='+escape(value)+';'
    }
    setCookie('username','gaoli')
    var ajaxData = {
    "ChannelConfig": [
        {
            "Channel": 0,
            "Baud": "1000000",
            "ListenOnly": false,
            "CCP": [],
            "XCP": [
                {
                    "RequestId": 2032,
                    "ResponseId": 2033,
                    "ByteOrder": "LSB_FIRST",
                    "DAQ": [
                        {
                            "MsgId": 2033,
                            "DaqNumber": 0,
                            "EventChannel": 0,
                            "FirstPID": 0,
                            "CycleTime": 15,
                            "Priority": 7,
                            "Measurement": [
                                {
                                    "Name": "wuccnt_msg",
                                    "Address": 1610638849,
                                    "Type": "UBYTE",
                                    "Conversion": {
                                        "Type": "RAT_FUNC",
                                        "Param": "[0,1,0,0,0,1]",
                                        "Unit": ""
                                    }
                                },
                                {
                                    "Name": "wub",
                                    "Address": 1610638328,
                                    "Type": "UBYTE",
                                    "Conversion": {
                                        "Type": "RAT_FUNC",
                                        "Param": "[0,10.6157112526539,0,0,0,1]",
                                        "Unit": "V"
                                    }
                                }
                            ]
                        },
                        {
                            "MsgId": 2033,
                            "DaqNumber": 1,
                            "EventChannel": 1,
                            "FirstPID": 15,
                            "CycleTime": 10,
                            "Priority": 6,
                            "Measurement": [
                                {
                                    "Name": "zlrslb_w_msg",
                                    "Address": 1610629260,
                                    "Type": "UWORD",
                                    "Conversion": {
                                        "Type": "RAT_FUNC",
                                        "Param": "[0,100,0,0,0,1]",
                                        "Unit": "s"
                                    }
                                },
                                {
                                    "Name": "zlrs_w_msg",
                                    "Address": 1610629266,
                                    "Type": "UWORD",
                                    "Conversion": {
                                        "Type": "RAT_FUNC",
                                        "Param": "[0,0,65536,0,100,0]",
                                        "Unit": "s"
                                    }
                                },
                                {
                                    "Name": "zlrsb_w_msg",
                                    "Address": 1610629264,
                                    "Type": "UWORD",
                                    "Conversion": {
                                        "Type": "RAT_FUNC",
                                        "Param": "[0,0,65536,0,100,0]",
                                        "Unit": "s"
                                    }
                                }
                            ]
                        },
                        {
                            "MsgId": 2033,
                            "DaqNumber": 2,
                            "EventChannel": 2,
                            "FirstPID": 29,
                            "CycleTime": 100,
                            "Priority": 5,
                            "Measurement": [
                                {
                                    "Name": "nmot_w",
                                    "Address": 1879069234,
                                    "Type": "UWORD",
                                    "Conversion": {
                                        "Type": "RAT_FUNC",
                                        "Param": "[0,1,0,0,0,0.25]",
                                        "Unit": "1/min"
                                    }
                                },
                                {
                                    "Name": "zzylmdmess",
                                    "Address": 1879063418,
                                    "Type": "UBYTE",
                                    "Conversion": {
                                        "Type": "RAT_FUNC",
                                        "Param": "[0,1,0,0,0,1]",
                                        "Unit": ""
                                    }
                                }
                            ]
                        }
                    ]
                }
            ],
            "UDS": [],
            "BMR": {
                "Filter": true,
                "MsgID": []
            }
        }
    ],
    "PreTrigger": 0,
    "PostTrigger": 0,
    "RecSegmentType": "size",
    "RecSegmentVal": 100,
    "AutoSleepTime": 0
};
    // ajax发送json,接受json
    $("#myjson").click(function () {
        $.ajax({
            type: "POST",
            url: "http://127.0.0.1:5000/form_data",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify(ajaxData),
            dataType: "json",
            success: function (message) {
                console.log("提交成功" + JSON.stringify(message) + status);
            },
            error: function (message) {
                alert("提交失败");
            }
        });
    });

flask代码

#对form表单发送数据
@app.route("/form_data", methods=['GET', 'POST'])
def myform():
    if request.method == "GET":
        return render_template("GW.html")
  # POST请求
    if request.method == "POST":
        pprint.pprint(request.json)
    name=request.cookies.get("username")
    print(name)
#转换lab
#存db

    return  {"asd":123423}