MCPcopy
hub / github.com/cherrypy/cherrypy / encode_multipart_formdata

Function encode_multipart_formdata

cherrypy/test/test_http.py:43–66  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

41
42
43def 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
69class HTTPTests(helper.CPWebCase):

Calls 3

encode_filenameFunction · 0.85
formatMethod · 0.80
joinMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…