當前位置: 首頁>>代碼示例>>Python>>正文


Python _compat.BytesIO方法代碼示例

本文整理匯總了Python中werkzeug._compat.BytesIO方法的典型用法代碼示例。如果您正苦於以下問題:Python _compat.BytesIO方法的具體用法?Python _compat.BytesIO怎麽用?Python _compat.BytesIO使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在werkzeug._compat的用法示例。


在下文中一共展示了_compat.BytesIO方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_headers

# 需要導入模塊: from werkzeug import _compat [as 別名]
# 或者: from werkzeug._compat import BytesIO [as 別名]
def test_headers(self):
        data = (b'--foo\r\n'
                b'Content-Disposition: form-data; name="foo"; filename="foo.txt"\r\n'
                b'X-Custom-Header: blah\r\n'
                b'Content-Type: text/plain; charset=utf-8\r\n\r\n'
                b'file contents, just the contents\r\n'
                b'--foo--')
        req = Request.from_values(input_stream=BytesIO(data),
                                  content_length=len(data),
                                  content_type='multipart/form-data; boundary=foo',
                                  method='POST')
        foo = req.files['foo']
        self.assert_strict_equal(foo.mimetype, 'text/plain')
        self.assert_strict_equal(foo.mimetype_params, {'charset': 'utf-8'})
        self.assert_strict_equal(foo.headers['content-type'], foo.content_type)
        self.assert_strict_equal(foo.content_type, 'text/plain; charset=utf-8')
        self.assert_strict_equal(foo.headers['x-custom-header'], 'blah') 
開發者ID:GeekTrainer,項目名稱:Flask,代碼行數:19,代碼來源:formparser.py

示例2: test_nonstandard_line_endings

# 需要導入模塊: from werkzeug import _compat [as 別名]
# 或者: from werkzeug._compat import BytesIO [as 別名]
def test_nonstandard_line_endings(self):
        for nl in b'\n', b'\r', b'\r\n':
            data = nl.join((
                b'--foo',
                b'Content-Disposition: form-data; name=foo',
                b'',
                b'this is just bar',
                b'--foo',
                b'Content-Disposition: form-data; name=bar',
                b'',
                b'blafasel',
                b'--foo--'
            ))
            req = Request.from_values(input_stream=BytesIO(data),
                                      content_length=len(data),
                                      content_type='multipart/form-data; '
                                      'boundary=foo', method='POST')
            self.assert_strict_equal(req.form['foo'], u'this is just bar')
            self.assert_strict_equal(req.form['bar'], u'blafasel') 
開發者ID:GeekTrainer,項目名稱:Flask,代碼行數:21,代碼來源:formparser.py

示例3: test_failures

# 需要導入模塊: from werkzeug import _compat [as 別名]
# 或者: from werkzeug._compat import BytesIO [as 別名]
def test_failures(self):
        def parse_multipart(stream, boundary, content_length):
            parser = formparser.MultiPartParser(content_length)
            return parser.parse(stream, boundary, content_length)
        self.assert_raises(ValueError, parse_multipart, BytesIO(), b'broken  ', 0)

        data = b'--foo\r\n\r\nHello World\r\n--foo--'
        self.assert_raises(ValueError, parse_multipart, BytesIO(data), b'foo', len(data))

        data = b'--foo\r\nContent-Disposition: form-field; name=foo\r\n' \
               b'Content-Transfer-Encoding: base64\r\n\r\nHello World\r\n--foo--'
        self.assert_raises(ValueError, parse_multipart, BytesIO(data), b'foo', len(data))

        data = b'--foo\r\nContent-Disposition: form-field; name=foo\r\n\r\nHello World\r\n'
        self.assert_raises(ValueError, parse_multipart, BytesIO(data), b'foo', len(data))

        x = formparser.parse_multipart_headers(['foo: bar\r\n', ' x test\r\n'])
        self.assert_strict_equal(x['foo'], 'bar\n x test')
        self.assert_raises(ValueError, formparser.parse_multipart_headers,
                           ['foo: bar\r\n', ' x test']) 
開發者ID:GeekTrainer,項目名稱:Flask,代碼行數:22,代碼來源:formparser.py

示例4: default_stream_factory

# 需要導入模塊: from werkzeug import _compat [as 別名]
# 或者: from werkzeug._compat import BytesIO [as 別名]
def default_stream_factory(total_content_length, filename, content_type,
                           content_length=None):
    """The stream factory that is used per default."""
    max_size = 1024 * 500
    if SpooledTemporaryFile is not None:
        return SpooledTemporaryFile(max_size=max_size, mode='wb+')
    if total_content_length is None or total_content_length > max_size:
        return TemporaryFile('wb+')
    return BytesIO() 
開發者ID:ryfeus,項目名稱:lambda-packs,代碼行數:11,代碼來源:formparser.py

示例5: test_missing_multipart_boundary

# 需要導入模塊: from werkzeug import _compat [as 別名]
# 或者: from werkzeug._compat import BytesIO [as 別名]
def test_missing_multipart_boundary(self):
        data = (b'--foo\r\nContent-Disposition: form-field; name=foo\r\n\r\n'
                b'Hello World\r\n'
                b'--foo\r\nContent-Disposition: form-field; name=bar\r\n\r\n'
                b'bar=baz\r\n--foo--')
        req = Request.from_values(input_stream=BytesIO(data),
                                  content_length=len(data),
                                  content_type='multipart/form-data',
                                  method='POST')
        self.assert_equal(req.form, {}) 
開發者ID:GeekTrainer,項目名稱:Flask,代碼行數:12,代碼來源:formparser.py

示例6: test_large_file

# 需要導入模塊: from werkzeug import _compat [as 別名]
# 或者: from werkzeug._compat import BytesIO [as 別名]
def test_large_file(self):
        data = b'x' * (1024 * 600)
        req = Request.from_values(data={'foo': (BytesIO(data), 'test.txt')},
                                  method='POST')
        # make sure we have a real file here, because we expect to be
        # on the disk.  > 1024 * 500
        self.assert_true(hasattr(req.files['foo'].stream, u'fileno'))
        # close file to prevent fds from leaking
        req.files['foo'].close() 
開發者ID:GeekTrainer,項目名稱:Flask,代碼行數:11,代碼來源:formparser.py

示例7: test_streaming_parse

# 需要導入模塊: from werkzeug import _compat [as 別名]
# 或者: from werkzeug._compat import BytesIO [as 別名]
def test_streaming_parse(self):
        data = b'x' * (1024 * 600)
        class StreamMPP(formparser.MultiPartParser):
            def parse(self, file, boundary, content_length):
                i = iter(self.parse_lines(file, boundary, content_length))
                one = next(i)
                two = next(i)
                return self.cls(()), {'one': one, 'two': two}
        class StreamFDP(formparser.FormDataParser):
            def _sf_parse_multipart(self, stream, mimetype,
                                    content_length, options):
                form, files = StreamMPP(
                    self.stream_factory, self.charset, self.errors,
                    max_form_memory_size=self.max_form_memory_size,
                    cls=self.cls).parse(stream, options.get('boundary').encode('ascii'),
                                        content_length)
                return stream, form, files
            parse_functions = {}
            parse_functions.update(formparser.FormDataParser.parse_functions)
            parse_functions['multipart/form-data'] = _sf_parse_multipart
        class StreamReq(Request):
            form_data_parser_class = StreamFDP
        req = StreamReq.from_values(data={'foo': (BytesIO(data), 'test.txt')},
                                    method='POST')
        self.assert_strict_equal('begin_file', req.files['one'][0])
        self.assert_strict_equal(('foo', 'test.txt'), req.files['one'][1][1:])
        self.assert_strict_equal('cont', req.files['two'][0])
        self.assert_strict_equal(data, req.files['two'][1]) 
開發者ID:GeekTrainer,項目名稱:Flask,代碼行數:30,代碼來源:formparser.py

示例8: test_end_of_file

# 需要導入模塊: from werkzeug import _compat [as 別名]
# 或者: from werkzeug._compat import BytesIO [as 別名]
def test_end_of_file(self):
        # This test looks innocent but it was actually timeing out in
        # the Werkzeug 0.5 release version (#394)
        data = (
            b'--foo\r\n'
            b'Content-Disposition: form-data; name="test"; filename="test.txt"\r\n'
            b'Content-Type: text/plain\r\n\r\n'
            b'file contents and no end'
        )
        data = Request.from_values(input_stream=BytesIO(data),
                                   content_length=len(data),
                                   content_type='multipart/form-data; boundary=foo',
                                   method='POST')
        self.assert_true(not data.files)
        self.assert_true(not data.form) 
開發者ID:GeekTrainer,項目名稱:Flask,代碼行數:17,代碼來源:formparser.py

示例9: test_extra_newline

# 需要導入模塊: from werkzeug import _compat [as 別名]
# 或者: from werkzeug._compat import BytesIO [as 別名]
def test_extra_newline(self):
        # this test looks innocent but it was actually timeing out in
        # the Werkzeug 0.5 release version (#394)
        data = (
            b'\r\n\r\n--foo\r\n'
            b'Content-Disposition: form-data; name="foo"\r\n\r\n'
            b'a string\r\n'
            b'--foo--'
        )
        data = Request.from_values(input_stream=BytesIO(data),
                                   content_length=len(data),
                                   content_type='multipart/form-data; boundary=foo',
                                   method='POST')
        self.assert_true(not data.files)
        self.assert_strict_equal(data.form['foo'], u'a string') 
開發者ID:GeekTrainer,項目名稱:Flask,代碼行數:17,代碼來源:formparser.py

示例10: test_bad_newline_bad_newline_assumption

# 需要導入模塊: from werkzeug import _compat [as 別名]
# 或者: from werkzeug._compat import BytesIO [as 別名]
def test_bad_newline_bad_newline_assumption(self):
        class ISORequest(Request):
            charset = 'latin1'
        contents = b'U2vlbmUgbORu'
        data = b'--foo\r\nContent-Disposition: form-data; name="test"\r\n' \
               b'Content-Transfer-Encoding: base64\r\n\r\n' + \
               contents + b'\r\n--foo--'
        req = ISORequest.from_values(input_stream=BytesIO(data),
                                     content_length=len(data),
                                     content_type='multipart/form-data; boundary=foo',
                                     method='POST')
        self.assert_strict_equal(req.form['test'], u'Sk\xe5ne l\xe4n') 
開發者ID:GeekTrainer,項目名稱:Flask,代碼行數:14,代碼來源:formparser.py

示例11: test_limited_stream_disconnection

# 需要導入模塊: from werkzeug import _compat [as 別名]
# 或者: from werkzeug._compat import BytesIO [as 別名]
def test_limited_stream_disconnection(self):
        io = BytesIO(b'A bit of content')

        # disconnect detection on out of bytes
        stream = wsgi.LimitedStream(io, 255)
        with self.assert_raises(ClientDisconnected):
            stream.read()

        # disconnect detection because file close
        io = BytesIO(b'x' * 255)
        io.close()
        stream = wsgi.LimitedStream(io, 255)
        with self.assert_raises(ClientDisconnected):
            stream.read() 
開發者ID:GeekTrainer,項目名稱:Flask,代碼行數:16,代碼來源:wsgi.py

示例12: test_multi_part_line_breaks_bytes

# 需要導入模塊: from werkzeug import _compat [as 別名]
# 或者: from werkzeug._compat import BytesIO [as 別名]
def test_multi_part_line_breaks_bytes(self):
        data = b'abcdef\r\nghijkl\r\nmnopqrstuvwxyz\r\nABCDEFGHIJK'
        test_stream = BytesIO(data)
        lines = list(wsgi.make_line_iter(test_stream, limit=len(data),
                                         buffer_size=16))
        self.assert_equal(lines, [b'abcdef\r\n', b'ghijkl\r\n',
                                  b'mnopqrstuvwxyz\r\n', b'ABCDEFGHIJK'])

        data = b'abc\r\nThis line is broken by the buffer length.' \
            b'\r\nFoo bar baz'
        test_stream = BytesIO(data)
        lines = list(wsgi.make_line_iter(test_stream, limit=len(data),
                                         buffer_size=24))
        self.assert_equal(lines, [b'abc\r\n', b'This line is broken by the '
                                  b'buffer length.\r\n', b'Foo bar baz']) 
開發者ID:GeekTrainer,項目名稱:Flask,代碼行數:17,代碼來源:wsgi.py

示例13: test_make_chunk_iter_bytes

# 需要導入模塊: from werkzeug import _compat [as 別名]
# 或者: from werkzeug._compat import BytesIO [as 別名]
def test_make_chunk_iter_bytes(self):
        data = [b'abcdefXghi', b'jklXmnopqrstuvwxyzX', b'ABCDEFGHIJK']
        rv = list(wsgi.make_chunk_iter(data, 'X'))
        self.assert_equal(rv, [b'abcdef', b'ghijkl', b'mnopqrstuvwxyz',
                               b'ABCDEFGHIJK'])

        data = b'abcdefXghijklXmnopqrstuvwxyzXABCDEFGHIJK'
        test_stream = BytesIO(data)
        rv = list(wsgi.make_chunk_iter(test_stream, 'X', limit=len(data),
                                       buffer_size=4))
        self.assert_equal(rv, [b'abcdef', b'ghijkl', b'mnopqrstuvwxyz',
                               b'ABCDEFGHIJK']) 
開發者ID:GeekTrainer,項目名稱:Flask,代碼行數:14,代碼來源:wsgi.py

示例14: test_streamed_url_decoding

# 需要導入模塊: from werkzeug import _compat [as 別名]
# 或者: from werkzeug._compat import BytesIO [as 別名]
def test_streamed_url_decoding(self):
        item1 = u'a' * 100000
        item2 = u'b' * 400
        string = ('a=%s&b=%s&c=%s' % (item1, item2, item2)).encode('ascii')
        gen = urls.url_decode_stream(BytesIO(string), limit=len(string),
                                     return_iterator=True)
        self.assert_strict_equal(next(gen), ('a', item1))
        self.assert_strict_equal(next(gen), ('b', item2))
        self.assert_strict_equal(next(gen), ('c', item2))
        self.assert_raises(StopIteration, lambda: next(gen)) 
開發者ID:GeekTrainer,項目名稱:Flask,代碼行數:12,代碼來源:urls.py

示例15: test_file_no_content_type

# 需要導入模塊: from werkzeug import _compat [as 別名]
# 或者: from werkzeug._compat import BytesIO [as 別名]
def test_file_no_content_type(self):
        data = (
            b'--foo\r\n'
            b'Content-Disposition: form-data; name="test"; filename="test.txt"\r\n\r\n'
            b'file contents\r\n--foo--'
        )
        data = Request.from_values(input_stream=BytesIO(data),
                                   content_length=len(data),
                                   content_type='multipart/form-data; boundary=foo',
                                   method='POST')
        self.assert_equal(data.files['test'].filename, 'test.txt')
        self.assert_strict_equal(data.files['test'].read(), b'file contents') 
開發者ID:GeekTrainer,項目名稱:Flask,代碼行數:14,代碼來源:formparser.py


注:本文中的werkzeug._compat.BytesIO方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。