| 1 |
"""Utility functions for HTTP multipart/form-data requests. |
| 2 |
|
| 3 |
It includes functions to post fields and files to an HTTP host and to encode |
| 4 |
data for multipart form submissions. |
| 5 |
""" |
| 6 |
# http://code.activestate.com/recipes/146306-http-client-to-post-using-multipartform-data/ |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
import http.client |
| 11 |
import mimetypes |
| 12 |
import uuid |
| 13 |
|
| 14 |
|
| 15 |
def post_multipart( |
| 16 |
host: str, |
| 17 |
selector: str, |
| 18 |
fields: list | tuple, |
| 19 |
files: list | tuple, |
| 20 |
charset: str = "utf-8", |
| 21 |
) -> bytes: |
| 22 |
"""Post fields and files to an http host as multipart/form-data. |
| 23 |
|
| 24 |
Args: |
| 25 |
host: The host to post to. |
| 26 |
selector: The URL path to post to. |
| 27 |
fields: A sequence of (name, value) elements for regular form fields. |
| 28 |
files: A sequence of (name, filename, value) elements for data to be |
| 29 |
uploaded as files. |
| 30 |
charset: The character set to use for encoding the fields and files. |
| 31 |
|
| 32 |
Returns: |
| 33 |
bytes: The server's response page. |
| 34 |
""" |
| 35 |
content_type, body = encode_multipart_formdata(fields, files, charset) |
| 36 |
h = http.client.HTTPConnection(host) |
| 37 |
h.putrequest("POST", selector) |
| 38 |
h.putheader("Content-Type", content_type) |
| 39 |
h.putheader("Content-Length", str(len(body))) |
| 40 |
h.endheaders() |
| 41 |
h.send(body) |
| 42 |
resp = h.getresponse() |
| 43 |
return resp.read() |
| 44 |
|
| 45 |
|
| 46 |
def encode_multipart_formdata( |
| 47 |
fields: tuple | list, files: tuple | list, charset: str = "utf-8" |
| 48 |
) -> tuple[bytes, bytes]: |
| 49 |
"""Encode fields and files for multipart/form-data. |
| 50 |
|
| 51 |
Args: |
| 52 |
fields (tuple | list): A sequence of (name, value) elements for regular |
| 53 |
form fields. |
| 54 |
files (tuple | list): A sequence of (name, filename, value) elements |
| 55 |
for data to be uploaded as files. |
| 56 |
charset (str): The character set to use for encoding the fields and |
| 57 |
files. |
| 58 |
|
| 59 |
Returns: |
| 60 |
tuple[bytes, bytes]: The content type and the body. Ready for |
| 61 |
http.client.HTTP instance. |
| 62 |
""" |
| 63 |
boundary = b"----=_NextPart_" + uuid.uuid1().bytes |
| 64 |
crlf = b"\r\n" |
| 65 |
l = [] |
| 66 |
for key, value in fields: |
| 67 |
if isinstance(key, str): |
| 68 |
key = key.encode(charset) |
| 69 |
if isinstance(value, str): |
| 70 |
value = value.encode(charset) |
| 71 |
|
| 72 |
l.append(b"--" + boundary) |
| 73 |
l.append(b'Content-Disposition: form-data; name="' + key + b'"') |
| 74 |
l.append(b"Content-Type: text/plain; charset=" + charset.encode(charset)) |
| 75 |
l.append(b"") |
| 76 |
l.append(value) |
| 77 |
|
| 78 |
for key, filename, value in files: |
| 79 |
if isinstance(key, str): |
| 80 |
key = key.encode(charset) |
| 81 |
if isinstance(filename, str): |
| 82 |
filename = filename.encode(charset) |
| 83 |
if isinstance(value, str): |
| 84 |
value = value.encode(charset) |
| 85 |
|
| 86 |
l.append(b"--" + boundary) |
| 87 |
l.append( |
| 88 |
b'Content-Disposition: form-data; name="' |
| 89 |
+ key |
| 90 |
+ b'"; filename="' |
| 91 |
+ filename |
| 92 |
+ b'"' |
| 93 |
) |
| 94 |
l.append(b"Content-Type: " + get_content_type(filename).encode(charset)) |
| 95 |
l.append(b"") |
| 96 |
l.append(value) |
| 97 |
|
| 98 |
l.append(b"--" + boundary + b"--") |
| 99 |
l.append(b"") |
| 100 |
body = crlf.join(l) |
| 101 |
content_type = b"multipart/form-data; boundary=" + boundary |
| 102 |
|
| 103 |
return content_type, body |
| 104 |
|
| 105 |
|
| 106 |
def get_content_type(filename: str | bytes) -> str: |
| 107 |
"""Get the content type of a file based on its filename. |
| 108 |
|
| 109 |
Args: |
| 110 |
filename (str | bytes): The filename to get the content type for. |
| 111 |
|
| 112 |
Returns: |
| 113 |
str: The content type of the file. |
| 114 |
""" |
| 115 |
if isinstance(filename, bytes): |
| 116 |
filename = filename.decode("utf-8") |
| 117 |
return mimetypes.guess_type(filename)[0] or "application/octet-stream" |