html与python后端交互,python后端中取表单

参考:http://www.manongjc.com/detail/13-owqqwhqvsqworkh.html

前端:





  
  测试表单



  
名字 :
年龄 :
密码:
性别: 男
内容: 提交表单:

python服务器

# coding=utf-8
from flask import Flask, request, jsonify
import urllib

app = Flask(__name__)


@app.route("/get/sum", methods=["GET", "POST"])
def get_sum():
    data = request.get_data()
    print("header {}".format(request.headers))
    print("data = ", data)

    text = data.decode("utf-8")
    sp = text.split("&")
    print("text = {}".format(text))

    print(urllib.parse.unquote(text, encoding="utf-8"))

    i = 0
    kv = {}
    for s in sp:
        print("{} s = {}".format(i, s))
        pp = s.split("=")
        if (len(pp) == 2):
            value = pp[1]
            kv[pp[0]] = value
            print(urllib.parse.unquote(value, encoding="utf-8"))

    print("kv {}".format(kv))
    info = {}
    info['name'] = "中文"
    info["age"] = 8928
    return jsonify(info)


if __name__ == "__main__":
    app.config["JSON_AS_ASCII"] = False
    app.run(host="127.0.0.1", port=8080)

 

结果:

html与python后端交互,python后端中取表单_第1张图片

点击“提交”按钮后结果(显示中文结果):

{"age":8928,"name":"中文"}

python服务器第二版,获取表单值

# coding=utf-8
from flask import Flask, request, jsonify
import urllib

app = Flask(__name__)


@app.route("/get/sum", methods=["GET", "POST"])
def get_sum():
    print("header {}".format(request.headers))
    print("args ", request.args)
    print("form {}".format(request.form.to_dict()))
    for k, v in request.form.to_dict().items():
        print("k = ", k, ", v = ", v)

    info = {}
    info['name'] = "中文"
    info["age"] = 8928
    return jsonify(info)


if __name__ == "__main__":
    app.config["JSON_AS_ASCII"] = False
    app.run(host="127.0.0.1", port=8080)

结果:

header Host: localhost:8080
Connection: keep-alive
Content-Length: 116
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
Origin: null
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: cross-site
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9


args  ImmutableMultiDict([])
form {'mname': 'myname', 'mage': '833', 'mpassword': '1314', 'sex': 'male', 'mcontent': 'input++= you are a good boy.你好啊'}
k =  mname , v =  myname
k =  mage , v =  833
k =  mpassword , v =  1314
k =  sex , v =  male
k =  mcontent , v =  input++= you are a good boy.你好啊
127.0.0.1 - - [15/Sep/2020 21:19:50] "POST /get/sum HTTP/1.1" 200 -

 

你可能感兴趣的:(h5,python)