Return (content_type, body) ready for httplib.HTTP instance. files: a sequence of (name, filename, value) tuples for multipart uploads. filename can be a string or a tuple ('filename string', 'encoding')
(files)
| 41 | |
| 42 | |
| 43 | def encode_multipart_formdata(files): |
| 44 | """Return (content_type, body) ready for httplib.HTTP instance. |
| 45 | |
| 46 | files: a sequence of (name, filename, value) tuples for multipart uploads. |
| 47 | filename can be a string or a tuple ('filename string', 'encoding') |
| 48 | """ |
| 49 | BOUNDARY = '________ThIs_Is_tHe_bouNdaRY_$' |
| 50 | L = [] |
| 51 | for key, filename, value in files: |
| 52 | L.append('--' + BOUNDARY) |
| 53 | |
| 54 | fn_key, encoded = encode_filename(filename) |
| 55 | tmpl = \ |
| 56 | 'Content-Disposition: form-data; name="{key}"; {fn_key}={encoded}' |
| 57 | L.append(tmpl.format(**locals())) |
| 58 | ct = mimetypes.guess_type(filename)[0] or 'application/octet-stream' |
| 59 | L.append('Content-Type: %s' % ct) |
| 60 | L.append('') |
| 61 | L.append(value) |
| 62 | L.append('--' + BOUNDARY + '--') |
| 63 | L.append('') |
| 64 | body = '\r\n'.join(L) |
| 65 | content_type = 'multipart/form-data; boundary=%s' % BOUNDARY |
| 66 | return content_type, body |
| 67 | |
| 68 | |
| 69 | class HTTPTests(helper.CPWebCase): |
no test coverage detected
searching dependent graphs…