Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.45 KB | None | 0 0
  1.     def multipart (self, fields):
  2.         boundary = uuid4().hex
  3.         blen = len(boundary)
  4.         tlen = 0 # Length of the entire body
  5.         w = lambda s, *d: s.write("".join(d))
  6.         pre, mid, post = stringio(None), [], stringio(None)
  7.         for key, value in fields.items():
  8.             if hasattr(value, "read"): continue # First iteration: fields
  9.             w(pre, "--", boundary, "\r\n")
  10.             w(pre, "Content-Disposition: form-data; ")
  11.             w(pre, 'name="', key, '"\r\n\r\n', value)
  12.             tlen += 47 + blen + len(key) + len(value)
  13.         for key, fd in fields.items():
  14.             if not hasattr(fd, "read"): continue # Second iteration: streams
  15.             name = getattr(fd, "name", key)
  16.             ct = filetypes[name] or "application/octet-stream"
  17.             wf = stringio(None)
  18.             w(wf, "--", boundary, "\r\n")
  19.             w(wf, "Content-Disposition: form-data; ")
  20.             w(wf, 'name="', key, '"; filename="', name, '"\r\n')
  21.             w(wf, "Content-Type: ", ct, "\r\n\r\n")
  22.             mid.append(wf)
  23.             mid.append(fd)
  24.             tlen += 76 + blen + len(ct) + len(key) + len(name)
  25.             tlen += fstat(fd.fileno()).st_size
  26.         w(post, "--", boundary, "--\r\n\r\n")
  27.         tlen += 8 + blen
  28.         mid.append(post)
  29.         c = iochain(None, pre, *mid)
  30.         ct = "multipart/form-data; boundary=" + boundary
  31.         return c, {"Content-Type": ct, "Content-Length": tlen}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement