form.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import warnings
  2. import six
  3. if six.PY2:
  4. from cStringIO import StringIO as BytesIO
  5. else:
  6. from io import BytesIO
  7. from w3lib.util import unicode_to_str
  8. def encode_multipart(data):
  9. r"""
  10. .. warning::
  11. This function is deprecated and will be removed in future.
  12. Please use ``urllib3.filepost.encode_multipart_formdata`` instead.
  13. Encode the given data to be used in a multipart HTTP POST.
  14. `data` is a dictionary where keys are the field name, and values are
  15. either strings or tuples as `(filename, content)` for file uploads.
  16. This code is based on :class:`distutils.command.upload`.
  17. Returns a `(body, boundary)` tuple where `body` is binary body value,
  18. and `boundary` is the boundary used (as native string).
  19. >>> import w3lib.form
  20. >>> w3lib.form.encode_multipart({'key': 'value'})
  21. ('\r\n----------------GHSKFJDLGDS7543FJKLFHRE75642756743254\r\nContent-Disposition: form-data; name="key"\r\n\r\nvalue\r\n----------------GHSKFJDLGDS7543FJKLFHRE75642756743254--\r\n', '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254')
  22. >>> w3lib.form.encode_multipart({'key1': 'value1', 'key2': 'value2'}) # doctest: +SKIP
  23. ('\r\n----------------GHSKFJDLGDS7543FJKLFHRE75642756743254\r\nContent-Disposition: form-data; name="key2"\r\n\r\nvalue2\r\n----------------GHSKFJDLGDS7543FJKLFHRE75642756743254\r\nContent-Disposition: form-data; name="key1"\r\n\r\nvalue1\r\n----------------GHSKFJDLGDS7543FJKLFHRE75642756743254--\r\n', '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254')
  24. >>> w3lib.form.encode_multipart({'somekey': ('path/to/filename', b'\xa1\xa2\xa3\xa4\r\n\r')})
  25. ('\r\n----------------GHSKFJDLGDS7543FJKLFHRE75642756743254\r\nContent-Disposition: form-data; name="somekey"; filename="path/to/filename"\r\n\r\n\xa1\xa2\xa3\xa4\r\n\r\r\n----------------GHSKFJDLGDS7543FJKLFHRE75642756743254--\r\n', '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254')
  26. >>>
  27. """
  28. warnings.warn(
  29. "`w3lib.form.encode_multipart` function is deprecated and "
  30. "will be removed in future releases. Please use "
  31. "`urllib3.filepost.encode_multipart_formdata` instead.",
  32. DeprecationWarning
  33. )
  34. # Build up the MIME payload for the POST data
  35. boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
  36. sep_boundary = b'\r\n--' + boundary.encode('ascii')
  37. end_boundary = sep_boundary + b'--'
  38. body = BytesIO()
  39. for key, value in data.items():
  40. title = u'\r\nContent-Disposition: form-data; name="%s"' % key
  41. # handle multiple entries for the same name
  42. if type(value) != type([]):
  43. value = [value]
  44. for value in value:
  45. if type(value) is tuple:
  46. title += u'; filename="%s"' % value[0]
  47. value = value[1]
  48. else:
  49. value = unicode_to_str(value) # in distutils: str(value).encode('utf-8')
  50. body.write(sep_boundary)
  51. body.write(title.encode('utf-8'))
  52. body.write(b"\r\n\r\n")
  53. body.write(value)
  54. body.write(end_boundary)
  55. body.write(b"\r\n")
  56. return body.getvalue(), boundary