python - Python (Flask) 中的 Get 和 Post 方法

标签 python http post flask get

我是 Flask 和网络开发的新手,我正在尝试创建一个简单的应用程序,其中在服务器上生成一个整数数组并将其发送到客户端。这是 app.py 中的一些示例(工作)代码:

from flask import Flask, render_template, request, url_for

import random


app = Flask(__name__)


@app.route('/')
def form():
    s_abc = [random.random() for _ in range(40)]
    return render_template('abc.html', s_abc=s_abc)

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

这里是 abc.html 的(有效)片段:

<div>
  {{s_abc}} 
</div>

我的问题是:

  1. 即使没有 GET/POST HTTP 方法,它是如何工作的?我认为服务器和客户端之间的通信需要 get/post http 方法(如此处所述:http://www.tutorialspoint.com/http/http_methods.htm)。然而,即使我没有写这样的代码,我的代码仍然有效:

    @app.route('/', methods=['GET'])
    
  2. 有没有办法重写它以便它使用 POST?显然,POST 更适合处理敏感数据,如下所述:http://blog.teamtreehouse.com/the-definitive-guide-to-get-vs-post

谢谢。

最佳答案

flask 的默认值是 GET。您可以使用方法来改变它:

@app.route('/', methods=['GET', 'POST'])

阅读文档:Flask 1.0.2 documentation

Web applications use different HTTP methods when accessing URLs. You should familiarize yourself with the HTTP methods as you work with Flask. By default, a route only answers to GET requests. You can use the methods argument of the route() decorator to handle different HTTP methods.

关于python - Python (Flask) 中的 Get 和 Post 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31081178/

相关文章:

python - 使用采样数据时,CRC32 函数如何工作?

python - 如何在 Pandas 中将多列合并到一个变量(即一个列表)中

php - 使用php脚本在Android应用程序中上传图像

http - HTTP 请求+响应的名称

php $_POST 数组并更新 MySQL

mysql - 如果帖子只有一个特定标签,则过滤查询中的帖子

python - 仅修改 Python3 的 Python 路径

python - 在 MySQL/SQLAlchemy 中计算事件持续时间的最佳方法?

python - 避免卡在 conn.getresponse() ( httplib.HTTPConnection )

javascript - 如何在 php 或 javascript 中创建带有请求的 POST