相关疑难解决方法(0)

Flask和Werkzeug:使用自定义标头测试发布请求

我目前正在使用来自http://flask.pocoo.org/docs/testing/的建议测试我的应用,但我想在帖子请求中添加标题.

我的要求目前是:

self.app.post('/v0/scenes/test/foo', data=dict(image=(StringIO('fake image'), 'image.png')))
Run Code Online (Sandbox Code Playgroud)

但我想在请求中添加一个内容-md5.这可能吗?

我的调查:

Flask Client(在flask/testing.py中)扩展了Werkzeug的客户端,在此处记录:http: //werkzeug.pocoo.org/docs/test/

如你所见,post用途open.但open只有:

Parameters: 
 as_tuple – Returns a tuple in the form (environ, result)
 buffered – Set this to True to buffer the application run. This will automatically close the application for you as well.
 follow_redirects – Set this to True if the Client should follow HTTP redirects.
Run Code Online (Sandbox Code Playgroud)

所以看起来它不受支持.但是,我怎么可以使用这样的功能呢?

python werkzeug flask

35
推荐指数
2
解决办法
2万
查看次数

Python - flask.request.files.stream应该是什么类型的?

在Flask(Flask-0.10.1通过pip安装)我试图像这样处理上传的文件

f = flask.request.files[input_name]
stream = f.stream
# then use the stream
Run Code Online (Sandbox Code Playgroud)

但令人困惑的是,在某些情况下stream是一个BytesIO实例,但也有机会成为一个file对象.

我已经用这种方式测试了

from flask import Flask, request
import cStringIO

app = Flask('test')

@app.route("/", methods=['POST'])
def index():
    if request.method == 'POST':
        f = request.files['file']
        print type(f.stream)

def send_file(client, name):
    with open(name, 'rb') as f:
        client.post('/', data={'file': (cStringIO.StringIO(f.read()), name)})

if __name__ == "__main__":
    with app.test_client() as client:
        send_file(client, 'homercat.png')
        send_file(client, 'megacat-2.png')
Run Code Online (Sandbox Code Playgroud)

它打印

<type '_io.BytesIO'>
<type 'file'>
Run Code Online (Sandbox Code Playgroud)

PNG文件来自github:

http://octodex.github.com/images/homercat.png http://octodex.github.com/images/megacat-2.png

我想知道为什么Flask会以这种方式行事.如果我希望上传的数据进入数据库,f.stream.read()两种情况下都可以调用吗?

python file-upload python-2.x flask

3
推荐指数
1
解决办法
4007
查看次数

标签 统计

flask ×2

python ×2

file-upload ×1

python-2.x ×1

werkzeug ×1