file_upload demo: write multi-part framing in one call
This commit is contained in:
parent
ab42d83714
commit
e92aa20f2d
|
@ -34,13 +34,15 @@ def multipart_producer(boundary, filenames, write):
|
||||||
|
|
||||||
for filename in filenames:
|
for filename in filenames:
|
||||||
filename_bytes = filename.encode()
|
filename_bytes = filename.encode()
|
||||||
yield write(b'--%s\r\n' % (boundary_bytes,))
|
|
||||||
yield write(b'Content-Disposition: form-data; name="%s"; filename="%s"\r\n' %
|
|
||||||
(filename_bytes, filename_bytes))
|
|
||||||
|
|
||||||
mtype = mimetypes.guess_type(filename)[0] or 'application/octet-stream'
|
mtype = mimetypes.guess_type(filename)[0] or 'application/octet-stream'
|
||||||
yield write(b'Content-Type: %s\r\n' % (mtype.encode(),))
|
buf = (
|
||||||
yield write(b'\r\n')
|
(b'--%s\r\n' % boundary_bytes) +
|
||||||
|
(b'Content-Disposition: form-data; name="%s"; filename="%s"\r\n' %
|
||||||
|
(filename_bytes, filename_bytes)) +
|
||||||
|
(b'Content-Type: %s\r\n' % mtype.encode()) +
|
||||||
|
b'\r\n'
|
||||||
|
)
|
||||||
|
yield write(buf)
|
||||||
with open(filename, 'rb') as f:
|
with open(filename, 'rb') as f:
|
||||||
while True:
|
while True:
|
||||||
# 16k at a time.
|
# 16k at a time.
|
||||||
|
@ -95,7 +97,6 @@ def put(filenames):
|
||||||
method='PUT',
|
method='PUT',
|
||||||
headers=headers,
|
headers=headers,
|
||||||
body_producer=producer)
|
body_producer=producer)
|
||||||
|
|
||||||
print(response)
|
print(response)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue