本文整理汇总了Python中requests_toolbelt.multipart.encoder.MultipartEncoder.read方法的典型用法代码示例。如果您正苦于以下问题:Python MultipartEncoder.read方法的具体用法?Python MultipartEncoder.read怎么用?Python MultipartEncoder.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类requests_toolbelt.multipart.encoder.MultipartEncoder
的用法示例。
在下文中一共展示了MultipartEncoder.read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: check_read_file_with_chunks
# 需要导入模块: from requests_toolbelt.multipart.encoder import MultipartEncoder [as 别名]
# 或者: from requests_toolbelt.multipart.encoder.MultipartEncoder import read [as 别名]
def check_read_file_with_chunks(self, file_size, read_size):
#print "===== Testing with file_size=",file_size,"read_size=",read_size
boundary="deterministic-test-boundary"
a_file = LargeFileMock(file_size)
parts = {'some_field': 'this is the value...',
'some_file': a_file.read(),
}
expected_bytes = encode_multipart_formdata(parts, boundary)[0]
content_length = len(expected_bytes)
# Now read from our encoder :
a_file = LargeFileMock(file_size)
parts = {'some_field': 'this is the value...',
'some_file': a_file,
}
encoder = MultipartEncoder(parts, boundary=boundary)
raw_bytes_count = 0
while True:
data = encoder.read(read_size)
if not data:
break
#print "read",len(data),"bytes : ",repr(data)
assert data == expected_bytes[raw_bytes_count:raw_bytes_count+len(data)]
raw_bytes_count += len(data)
#if raw_bytes_count != content_length:
# print "Test failed with file_size=",file_size,"and read_size=",read_size
assert raw_bytes_count == content_length
示例2: test_encodes_with_readable_data
# 需要导入模块: from requests_toolbelt.multipart.encoder import MultipartEncoder [as 别名]
# 或者: from requests_toolbelt.multipart.encoder.MultipartEncoder import read [as 别名]
def test_encodes_with_readable_data(self):
s = io.BytesIO(b'value')
m = MultipartEncoder([('field', s)], boundary=self.boundary)
assert m.read() == (
'--this-is-a-boundary\r\n'
'Content-Disposition: form-data; name="field"\r\n\r\n'
'value\r\n'
'--this-is-a-boundary--\r\n'
).encode()
示例3: test_reads_file_from_url_wrapper
# 需要导入模块: from requests_toolbelt.multipart.encoder import MultipartEncoder [as 别名]
# 或者: from requests_toolbelt.multipart.encoder.MultipartEncoder import read [as 别名]
def test_reads_file_from_url_wrapper(self):
s = requests.Session()
recorder = get_betamax(s)
url = ('https://stxnext.com/static/img/logo.830ebe551641.svg')
with recorder.use_cassette(
'file_for_download'):
m = MultipartEncoder(
[('field', 'foo'), ('file', FileFromURLWrapper(url))])
assert m.read() is not None
示例4: test_handles_empty_unicode_values
# 需要导入模块: from requests_toolbelt.multipart.encoder import MultipartEncoder [as 别名]
# 或者: from requests_toolbelt.multipart.encoder.MultipartEncoder import read [as 别名]
def test_handles_empty_unicode_values(self):
"""Verify that the Encoder can handle empty unicode strings.
See https://github.com/requests/toolbelt/issues/46 for
more context.
"""
fields = [(b'test'.decode('utf-8'), b''.decode('utf-8'))]
m = MultipartEncoder(fields=fields)
assert len(m.read()) > 0
示例5: test_accepts_custom_content_type
# 需要导入模块: from requests_toolbelt.multipart.encoder import MultipartEncoder [as 别名]
# 或者: from requests_toolbelt.multipart.encoder.MultipartEncoder import read [as 别名]
def test_accepts_custom_content_type(self):
"""Verify that the Encoder handles custom content-types.
See https://github.com/requests/toolbelt/issues/52
"""
fields = [
(b'test'.decode('utf-8'), (b'filename'.decode('utf-8'),
b'filecontent',
b'application/json'.decode('utf-8')))
]
m = MultipartEncoder(fields=fields)
output = m.read().decode('utf-8')
assert output.index('Content-Type: application/json\r\n') > 0
示例6: test_streams_its_data
# 需要导入模块: from requests_toolbelt.multipart.encoder import MultipartEncoder [as 别名]
# 或者: from requests_toolbelt.multipart.encoder.MultipartEncoder import read [as 别名]
def test_streams_its_data(self):
large_file = LargeFileMock()
parts = {'some field': 'value',
'some file': large_file,
}
encoder = MultipartEncoder(parts)
read_size = 1024 * 1024 * 128
while True:
read = encoder.read(read_size)
if not read:
break
assert encoder._buffer.tell() <= read_size
示例7: test_accepts_custom_headers
# 需要导入模块: from requests_toolbelt.multipart.encoder import MultipartEncoder [as 别名]
# 或者: from requests_toolbelt.multipart.encoder.MultipartEncoder import read [as 别名]
def test_accepts_custom_headers(self):
"""Verify that the Encoder handles custom headers.
See https://github.com/requests/toolbelt/issues/52
"""
fields = [
(b'test'.decode('utf-8'), (b'filename'.decode('utf-8'),
b'filecontent',
b'application/json'.decode('utf-8'),
{'X-My-Header': 'my-value'}))
]
m = MultipartEncoder(fields=fields)
output = m.read().decode('utf-8')
assert output.index('X-My-Header: my-value\r\n') > 0
示例8: test_streams_its_data
# 需要导入模块: from requests_toolbelt.multipart.encoder import MultipartEncoder [as 别名]
# 或者: from requests_toolbelt.multipart.encoder.MultipartEncoder import read [as 别名]
def test_streams_its_data(self):
large_file = LargeFileMock()
parts = {'some field': 'value',
'some file': large_file,
}
encoder = MultipartEncoder(parts)
total_size = encoder.len
read_size = 1024 * 1024 * 128
already_read = 0
while True:
read = encoder.read(read_size)
already_read += len(read)
if not read:
break
assert encoder._buffer.tell() <= read_size
assert already_read == total_size
示例9: test_regression_2
# 需要导入模块: from requests_toolbelt.multipart.encoder import MultipartEncoder [as 别名]
# 或者: from requests_toolbelt.multipart.encoder.MultipartEncoder import read [as 别名]
def test_regression_2(self):
"""Ensure issue #31 doesn't ever happen again."""
fields = {
"test": "t" * 8100
}
m = MultipartEncoder(fields=fields)
total_size = m.len
blocksize = 8192
read_so_far = 0
while True:
data = m.read(blocksize)
if not data:
break
read_so_far += len(data)
assert read_so_far == total_size
示例10: test_regresion_1
# 需要导入模块: from requests_toolbelt.multipart.encoder import MultipartEncoder [as 别名]
# 或者: from requests_toolbelt.multipart.encoder.MultipartEncoder import read [as 别名]
def test_regresion_1(self):
"""Ensure issue #31 doesn't ever happen again."""
fields = {
"test": "t" * 100
}
for x in range(30):
fields['f%d' % x] = (
'test', open('tests/test_multipart_encoder.py', 'rb')
)
m = MultipartEncoder(fields=fields)
total_size = m.len
blocksize = 8192
read_so_far = 0
while True:
data = m.read(blocksize)
if not data:
break
read_so_far += len(data)
assert read_so_far == total_size
示例11: test_handles_uncode_strings
# 需要导入模块: from requests_toolbelt.multipart.encoder import MultipartEncoder [as 别名]
# 或者: from requests_toolbelt.multipart.encoder.MultipartEncoder import read [as 别名]
def test_handles_uncode_strings(self):
s = b'this is a unicode string: \xc3\xa9 \xc3\xa1 \xc7\xab \xc3\xb3'
m = MultipartEncoder([
('field', s.decode('utf-8'))
])
assert m.read() is not None
示例12: test_handles_encoded_unicode_strings
# 需要导入模块: from requests_toolbelt.multipart.encoder import MultipartEncoder [as 别名]
# 或者: from requests_toolbelt.multipart.encoder.MultipartEncoder import read [as 别名]
def test_handles_encoded_unicode_strings(self):
m = MultipartEncoder([
('field',
b'this is a unicode string: \xc3\xa9 \xc3\xa1 \xc7\xab \xc3\xb3')
])
assert m.read() is not None
示例13: test_reads_open_file_objects_with_a_specified_filename
# 需要导入模块: from requests_toolbelt.multipart.encoder import MultipartEncoder [as 别名]
# 或者: from requests_toolbelt.multipart.encoder.MultipartEncoder import read [as 别名]
def test_reads_open_file_objects_with_a_specified_filename(self):
with open('setup.py', 'rb') as fd:
m = MultipartEncoder(
[('field', 'foo'), ('file', ('filename', fd, 'text/plain'))]
)
assert m.read() is not None
示例14: TestMultipartEncoder
# 需要导入模块: from requests_toolbelt.multipart.encoder import MultipartEncoder [as 别名]
# 或者: from requests_toolbelt.multipart.encoder.MultipartEncoder import read [as 别名]
class TestMultipartEncoder(unittest.TestCase):
def setUp(self):
self.parts = [('field', 'value'), ('other_field', 'other_value')]
self.boundary = 'this-is-a-boundary'
self.instance = MultipartEncoder(self.parts, boundary=self.boundary)
def test_to_string(self):
assert self.instance.to_string() == (
'--this-is-a-boundary\r\n'
'Content-Disposition: form-data; name="field"\r\n\r\n'
'value\r\n'
'--this-is-a-boundary\r\n'
'Content-Disposition: form-data; name="other_field"\r\n\r\n'
'other_value\r\n'
'--this-is-a-boundary--\r\n'
).encode()
def test_content_type(self):
expected = 'multipart/form-data; boundary=this-is-a-boundary'
assert self.instance.content_type == expected
def test_encodes_data_the_same(self):
encoded = encode_multipart_formdata(self.parts, self.boundary)[0]
assert encoded == self.instance.read()
def test_streams_its_data(self):
large_file = LargeFileMock(123456789)
parts = {'some field': 'value',
'some file': large_file,
}
encoder = MultipartEncoder(parts)
total_size = len(encoder)
read_size = 1024 * 1024 * 128
already_read = 0
while True:
read = encoder.read(read_size)
already_read += len(read)
if not read:
break
assert encoder._buffer.tell() <= read_size
assert already_read == total_size
def test_streams_its_data_with_correct_length(self):
for i in range(0, 100): # or more than 100 to increase fuzzing strength
file_size = random.randint(0, 12345)
if random.random() < 0.1:
file_size = 0 # sometimes we check with an empty file
self.check_read_file_with_chunks(file_size, read_size=1)
self.check_read_file_with_chunks(file_size, read_size=2)
self.check_read_file_with_chunks(file_size, read_size=3)
read_size = random.randint(0, 2*file_size)
self.check_read_file_with_chunks(file_size, read_size=1)
for read_size in range(file_size - 10, file_size + 200):
if read_size < -1 or read_size == 0:
continue
self.check_read_file_with_chunks(file_size, read_size)
def check_read_file_with_chunks(self, file_size, read_size):
#print "===== Testing with file_size=",file_size,"read_size=",read_size
boundary="deterministic-test-boundary"
a_file = LargeFileMock(file_size)
parts = {'some_field': 'this is the value...',
'some_file': a_file.read(),
}
expected_bytes = encode_multipart_formdata(parts, boundary)[0]
content_length = len(expected_bytes)
# Now read from our encoder :
a_file = LargeFileMock(file_size)
parts = {'some_field': 'this is the value...',
'some_file': a_file,
}
encoder = MultipartEncoder(parts, boundary=boundary)
raw_bytes_count = 0
while True:
data = encoder.read(read_size)
if not data:
break
#print "read",len(data),"bytes : ",repr(data)
assert data == expected_bytes[raw_bytes_count:raw_bytes_count+len(data)]
raw_bytes_count += len(data)
#if raw_bytes_count != content_length:
# print "Test failed with file_size=",file_size,"and read_size=",read_size
assert raw_bytes_count == content_length
def test_length_is_correct(self):
encoded = encode_multipart_formdata(self.parts, self.boundary)[0]
assert len(encoded) == len(self.instance)
def test_encodes_with_readable_data(self):
s = io.BytesIO(b'value')
m = MultipartEncoder([('field', s)], boundary=self.boundary)
assert m.read() == (
'--this-is-a-boundary\r\n'
'Content-Disposition: form-data; name="field"\r\n\r\n'
'value\r\n'
'--this-is-a-boundary--\r\n'
).encode()
#.........这里部分代码省略.........
示例15: test_read
# 需要导入模块: from requests_toolbelt.multipart.encoder import MultipartEncoder [as 别名]
# 或者: from requests_toolbelt.multipart.encoder.MultipartEncoder import read [as 别名]
def test_read(self):
new_encoder = MultipartEncoder(self.fields, self.boundary)
assert new_encoder.read() == self.monitor.read()