用Flask和Python 3测试文件上传 [英] Testing file upload with Flask and Python 3

查看:186
本文介绍了用Flask和Python 3测试文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Flask和Python 3.3,我知道支持仍然是实验性的,但是当我尝试测试文件上传时,我遇到了错误。我正在使用 unittest.TestCase ,并基于我在文档中看到的Python 2.7示例

  rv = self.app.post('/ add',data = dict(
file =(io.StringIO(this is a test),'test。 pdf'),
),follow_redirects = True)

得到

  TypeError:'str'不支持缓冲区接口

我已经尝试了一些io.StringIO的变体,但找不到任何可行的东西。任何帮助非常感谢!
$ b $ p完整的堆栈跟踪是

$ $ p $ $ $ $ $ $ Traceback(最近一次调用最后一次):
文件archive_tests.py,第44行,在test_add_transcript
),follow_redirects = True)
文件/ srv / transcript_archive / py3env / lib / python3 (* args,** kw)
文件/ srv / transcript_archive / py3env / lib / .3 / site-packages / werkzeug / python3.3 / site-packages / flask / testing.py,第108行,打开
follow_redirects = follow_redirects)
文件/srv/transcript_archive/py3env/lib/python3.3/site-packages /werkzeug/test.py,第725行,打开
environ = args [0] .get_environ()
文件/srv/transcript_archive/py3env/lib/python3.3/site-packages/ werkzeug / test.py,第535行,在get_environ
stream_encode_multipart(values,charset = self.charset)
文件/srv/transcript_archive/py3env/lib/python3.3/site-packages/werkzeug /test.py,第98行,在stream_encode_multipart
w rite_binary(块)
文件/srv/transcript_archive/py3env/lib/python3.3/site-packages/werkzeug/test.py,第59行,在write_binary
stream.write(string)
TypeError:'str'不支持缓冲区接口


解决方案

在Python 3中,您需要使用 io.BytesIO()(带有字节值)来模拟上传的文件:

  rv = self.app.post('/ add',data = dict(
file =(io.BytesIO(bthis is a test ),'test.pdf'),
),follow_redirects = True)

b'...'定义字节字面值的字符串。

在Python 2测试示例中, StringIO()对象包含一个字节字符串,而不是 unicode 值,而在Python 3中, io.BytesIO()是等效的。


I'm using Flask with Python 3.3 and I know support is still experimental but I'm running into errors when trying to test file uploads. I'm using unittest.TestCase and based on Python 2.7 examples I've seen in the docs I'm trying

rv = self.app.post('/add', data=dict(
                               file=(io.StringIO("this is a test"), 'test.pdf'),
                           ), follow_redirects=True)

and getting

TypeError: 'str' does not support the buffer interface

I've tried a few variations around io.StringIO but can't find anything that works. Any help is much appreciated!

The full stack trace is

Traceback (most recent call last):
  File "archive_tests.py", line 44, in test_add_transcript
    ), follow_redirects=True)
  File "/srv/transcript_archive/py3env/lib/python3.3/site-packages/werkzeug/test.py", line 771, in post
    return self.open(*args, **kw)
  File "/srv/transcript_archive/py3env/lib/python3.3/site-packages/flask/testing.py", line 108, in open
    follow_redirects=follow_redirects)
  File "/srv/transcript_archive/py3env/lib/python3.3/site-packages/werkzeug/test.py", line 725, in open
    environ = args[0].get_environ()
  File "/srv/transcript_archive/py3env/lib/python3.3/site-packages/werkzeug/test.py", line 535, in get_environ
    stream_encode_multipart(values, charset=self.charset)
  File "/srv/transcript_archive/py3env/lib/python3.3/site-packages/werkzeug/test.py", line 98, in stream_encode_multipart
    write_binary(chunk)
  File "/srv/transcript_archive/py3env/lib/python3.3/site-packages/werkzeug/test.py", line 59, in write_binary
    stream.write(string)
TypeError: 'str' does not support the buffer interface

解决方案

In Python 3, you need to use io.BytesIO() (with a bytes value) to simulate an uploaded file:

rv = self.app.post('/add', data=dict(
                               file=(io.BytesIO(b"this is a test"), 'test.pdf'),
                           ), follow_redirects=True)

Note the b'...' string defining a bytes literal.

In the Python 2 test examples, the StringIO() object holds a byte string, not a unicode value, and in Python 3, io.BytesIO() is the equivalent.

这篇关于用Flask和Python 3测试文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆