publishers_21.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. def encode_multipart_formdata(fields, files):
  2. """fields is a sequence of (name, value) elements for regular form fields.
  3. files is a sequence of (name, filename, value) elements for data to be uploaded as files
  4. Return (content_type, body) ready for httplib.HTTP instance
  5. """
  6. BOUNDARY = "GlobalNumberOfPiratesDecreasing-GlobalTemperatureIncreasing"
  7. CRLF = "\r\n"
  8. request = []
  9. for (key, value) in fields:
  10. request.extend(
  11. [
  12. "--" + BOUNDARY,
  13. 'Content-Disposition: form-data; name="%s"' % key,
  14. "",
  15. value,
  16. ]
  17. )
  18. for (key, filename, value) in files:
  19. request.extend(
  20. [
  21. "--" + BOUNDARY,
  22. 'Content-Disposition: form-data; name="%s"; filename="%s"'
  23. % (key, filename),
  24. "Content-Type: %s" % get_content_type(filename),
  25. "",
  26. value,
  27. ]
  28. )
  29. request.append("--" + BOUNDARY + "--")
  30. request.append("")
  31. body = CRLF.join(request)
  32. content_type = "multipart/form-data; boundary=%s" % BOUNDARY
  33. return content_type, body