k3httpmultipart
HTTP multipart form data encoder for building multipart/form-data requests.
k3httpmultipart is a component of pykit3 project: a python3 toolkit set.
Installation
pip install k3httpmultipart
Quick Start
import os
import k3httpmultipart
# Define form fields
fields = [
{
'name': 'text_field',
'value': 'hello world',
},
{
'name': 'file_field',
'value': [open('/path/to/file.txt'), os.path.getsize('/path/to/file.txt'), 'file.txt']
},
]
# Create multipart encoder
multipart = k3httpmultipart.Multipart()
# Get headers with Content-Type and Content-Length
headers = multipart.make_headers(fields)
# {'Content-Type': 'multipart/form-data; boundary=...', 'Content-Length': ...}
# Get body reader (generator)
body_reader = multipart.make_body_reader(fields)
# Read body content
body = ''.join(body_reader)
API Reference
k3httpmultipart
Multipart
Bases: object
:param block_size: It represents the size of each reading file :param boundary: a placeholder that represents out specified delimiter
Source code in k3httpmultipart/multipart.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
make_body_reader(fields)
Return a body according to the fields
:param fields: refer to the explanation above fields :return: a generator that represents the multipart request body
Source code in k3httpmultipart/multipart.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | |
make_headers(fields, headers=None)
Return a header according to the fields and headers
:param fields: is a list of the dict, and each elements contains `name`, `value` and `headers`,
headers is an optional argument
- name:
It's a string that represents field's name
- `value`:
The value represents field's content. The type of value can be a string or a
list, string indicates that the field is a normal string, However, there are
three arguments of list: `content`, `size` and `file_name`
- `content`:
The type of `content` can be string, reader, file object
The string type refers to the user want to upload a string. It takes the
string as the field body
The reader type refers to a generator. To read the contents of generator as
the field body
The file object type refers to a file object, To read the contents of file
as the field body
- `size`
`size` refers to the length of the content, When the type of `content` is a
string, size can be None
- `file_name`
`file_name` is an optional argument, if `file_name` is None, that indicates
that `content` is uploaded as a normal field, whereas, the field as a file
- `headers`:
a dict, key is the `field_header_name`, value is the `field_header_value`,
it contains user defined headers and the required headers, such as
'Content-Disposition' and 'Content-Type'
:param headers: a dict of http request headers, key is the `header_name`, value is the
header_value. It's a default argument and its default value is None
:return: a dict that represents the request headers
Source code in k3httpmultipart/multipart.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |
License
The MIT License (MIT) - Copyright (c) 2015 Zhang Yanpo (张炎泼)