k3httpmultipart 0.1.0__tar.gz → 0.1.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- k3httpmultipart-0.1.2/LICENSE +21 -0
- k3httpmultipart-0.1.2/PKG-INFO +140 -0
- k3httpmultipart-0.1.2/README.md +103 -0
- k3httpmultipart-0.1.2/__init__.py +15 -0
- k3httpmultipart-0.1.2/k3httpmultipart.egg-info/PKG-INFO +140 -0
- k3httpmultipart-0.1.2/k3httpmultipart.egg-info/SOURCES.txt +16 -0
- k3httpmultipart-0.1.2/k3httpmultipart.egg-info/requires.txt +17 -0
- k3httpmultipart-0.1.2/multipart.py +185 -0
- k3httpmultipart-0.1.2/pyproject.toml +60 -0
- {k3httpmultipart-0.1.0/k3httpmultipart → k3httpmultipart-0.1.2}/synopsis.py +19 -22
- k3httpmultipart-0.1.2/test/test_doctest.py +8 -0
- k3httpmultipart-0.1.2/test/test_k3httpmultipart.py +237 -0
- k3httpmultipart-0.1.0/PKG-INFO +0 -119
- k3httpmultipart-0.1.0/k3httpmultipart/__init__.py +0 -29
- k3httpmultipart-0.1.0/k3httpmultipart/multipart.py +0 -195
- k3httpmultipart-0.1.0/k3httpmultipart/setup.py +0 -23
- k3httpmultipart-0.1.0/k3httpmultipart.egg-info/PKG-INFO +0 -119
- k3httpmultipart-0.1.0/k3httpmultipart.egg-info/SOURCES.txt +0 -10
- k3httpmultipart-0.1.0/k3httpmultipart.egg-info/requires.txt +0 -2
- k3httpmultipart-0.1.0/setup.py +0 -23
- {k3httpmultipart-0.1.0 → k3httpmultipart-0.1.2}/k3httpmultipart.egg-info/dependency_links.txt +0 -0
- {k3httpmultipart-0.1.0 → k3httpmultipart-0.1.2}/k3httpmultipart.egg-info/top_level.txt +0 -0
- {k3httpmultipart-0.1.0 → k3httpmultipart-0.1.2}/setup.cfg +0 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 张炎泼
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: k3httpmultipart
|
|
3
|
+
Version: 0.1.2
|
|
4
|
+
Summary: HTTP multipart form data encoder
|
|
5
|
+
Author-email: Zhang Yanpo <drdr.xp@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/pykit3/k3httpmultipart
|
|
8
|
+
Project-URL: Documentation, https://k3httpmultipart.readthedocs.io
|
|
9
|
+
Keywords: http,multipart,form-data,upload
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Requires-Python: >=3.9
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: k3mime
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
25
|
+
Requires-Dist: ruff; extra == "dev"
|
|
26
|
+
Requires-Dist: coverage; extra == "dev"
|
|
27
|
+
Requires-Dist: k3fs; extra == "dev"
|
|
28
|
+
Provides-Extra: publish
|
|
29
|
+
Requires-Dist: build; extra == "publish"
|
|
30
|
+
Requires-Dist: twine; extra == "publish"
|
|
31
|
+
Requires-Dist: pk3; extra == "publish"
|
|
32
|
+
Provides-Extra: docs
|
|
33
|
+
Requires-Dist: mkdocs>=1.5; extra == "docs"
|
|
34
|
+
Requires-Dist: mkdocs-material>=9.0; extra == "docs"
|
|
35
|
+
Requires-Dist: mkdocstrings[python]>=0.24; extra == "docs"
|
|
36
|
+
Dynamic: license-file
|
|
37
|
+
|
|
38
|
+
# k3httpmultipart
|
|
39
|
+
|
|
40
|
+
[](https://github.com/pykit3/k3httpmultipart/actions/workflows/python-package.yml)
|
|
41
|
+
[](https://travis-ci.com/pykit3/k3httpmultipart)
|
|
42
|
+
[](https://k3httpmultipart.readthedocs.io/en/stable/?badge=stable)
|
|
43
|
+
[](https://pypi.org/project/k3httpmultipart)
|
|
44
|
+
|
|
45
|
+
This module provides some util methods to get multipart headers and body.
|
|
46
|
+
|
|
47
|
+
k3httpmultipart is a component of [pykit3] project: a python3 toolkit set.
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
# Name
|
|
51
|
+
|
|
52
|
+
k3httpmultipart
|
|
53
|
+
|
|
54
|
+
# Status
|
|
55
|
+
|
|
56
|
+
The library is considered production ready.
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
# Install
|
|
62
|
+
|
|
63
|
+
```
|
|
64
|
+
pip install k3httpmultipart
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
# Synopsis
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
|
|
71
|
+
import os
|
|
72
|
+
|
|
73
|
+
import k3httpmultipart
|
|
74
|
+
import k3fs
|
|
75
|
+
|
|
76
|
+
# http request headers
|
|
77
|
+
headers = {'Content-Length': 1200}
|
|
78
|
+
|
|
79
|
+
# http request fields
|
|
80
|
+
file_path = '/tmp/abc.txt'
|
|
81
|
+
k3fs.fwrite(file_path, '123456789')
|
|
82
|
+
fields = [
|
|
83
|
+
{
|
|
84
|
+
'name': 'aaa',
|
|
85
|
+
'value': 'abcde',
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
'name': 'bbb',
|
|
89
|
+
'value': [open(file_path), os.path.getsize(file_path), 'abc.txt']
|
|
90
|
+
},
|
|
91
|
+
]
|
|
92
|
+
|
|
93
|
+
# get http request headers
|
|
94
|
+
multipart = k3httpmultipart.Multipart()
|
|
95
|
+
res_headers = multipart.make_headers(fields, headers=headers)
|
|
96
|
+
|
|
97
|
+
print(res_headers)
|
|
98
|
+
|
|
99
|
+
#output:
|
|
100
|
+
#{
|
|
101
|
+
# 'Content-Type': 'multipart/form-data; boundary=FormBoundaryrGKCBY7',
|
|
102
|
+
# 'Conetnt-Length': 1200,
|
|
103
|
+
#}
|
|
104
|
+
|
|
105
|
+
# get http request body reader
|
|
106
|
+
multipart = k3httpmultipart.Multipart()
|
|
107
|
+
body_reader = multipart.make_body_reader(fields)
|
|
108
|
+
data = []
|
|
109
|
+
|
|
110
|
+
for body in body_reader:
|
|
111
|
+
data.append(body)
|
|
112
|
+
|
|
113
|
+
print(''.join(data))
|
|
114
|
+
|
|
115
|
+
#output:
|
|
116
|
+
#--FormBoundaryrGKCBY7
|
|
117
|
+
#Content-Disposition: form-data; name=aaa
|
|
118
|
+
#
|
|
119
|
+
#abcde
|
|
120
|
+
#--FormBoundaryrGKCBY7
|
|
121
|
+
#Content-Disposition: form-data; name=bbb; filename=abc.txt
|
|
122
|
+
#Content-Type: text/plain
|
|
123
|
+
#
|
|
124
|
+
#123456789
|
|
125
|
+
#--FormBoundaryrGKCBY7--
|
|
126
|
+
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
# Author
|
|
130
|
+
|
|
131
|
+
Zhang Yanpo (张炎泼) <drdr.xp@gmail.com>
|
|
132
|
+
|
|
133
|
+
# Copyright and License
|
|
134
|
+
|
|
135
|
+
The MIT License (MIT)
|
|
136
|
+
|
|
137
|
+
Copyright (c) 2015 Zhang Yanpo (张炎泼) <drdr.xp@gmail.com>
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
[pykit3]: https://github.com/pykit3
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# k3httpmultipart
|
|
2
|
+
|
|
3
|
+
[](https://github.com/pykit3/k3httpmultipart/actions/workflows/python-package.yml)
|
|
4
|
+
[](https://travis-ci.com/pykit3/k3httpmultipart)
|
|
5
|
+
[](https://k3httpmultipart.readthedocs.io/en/stable/?badge=stable)
|
|
6
|
+
[](https://pypi.org/project/k3httpmultipart)
|
|
7
|
+
|
|
8
|
+
This module provides some util methods to get multipart headers and body.
|
|
9
|
+
|
|
10
|
+
k3httpmultipart is a component of [pykit3] project: a python3 toolkit set.
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
# Name
|
|
14
|
+
|
|
15
|
+
k3httpmultipart
|
|
16
|
+
|
|
17
|
+
# Status
|
|
18
|
+
|
|
19
|
+
The library is considered production ready.
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
# Install
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
pip install k3httpmultipart
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
# Synopsis
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
|
|
34
|
+
import os
|
|
35
|
+
|
|
36
|
+
import k3httpmultipart
|
|
37
|
+
import k3fs
|
|
38
|
+
|
|
39
|
+
# http request headers
|
|
40
|
+
headers = {'Content-Length': 1200}
|
|
41
|
+
|
|
42
|
+
# http request fields
|
|
43
|
+
file_path = '/tmp/abc.txt'
|
|
44
|
+
k3fs.fwrite(file_path, '123456789')
|
|
45
|
+
fields = [
|
|
46
|
+
{
|
|
47
|
+
'name': 'aaa',
|
|
48
|
+
'value': 'abcde',
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
'name': 'bbb',
|
|
52
|
+
'value': [open(file_path), os.path.getsize(file_path), 'abc.txt']
|
|
53
|
+
},
|
|
54
|
+
]
|
|
55
|
+
|
|
56
|
+
# get http request headers
|
|
57
|
+
multipart = k3httpmultipart.Multipart()
|
|
58
|
+
res_headers = multipart.make_headers(fields, headers=headers)
|
|
59
|
+
|
|
60
|
+
print(res_headers)
|
|
61
|
+
|
|
62
|
+
#output:
|
|
63
|
+
#{
|
|
64
|
+
# 'Content-Type': 'multipart/form-data; boundary=FormBoundaryrGKCBY7',
|
|
65
|
+
# 'Conetnt-Length': 1200,
|
|
66
|
+
#}
|
|
67
|
+
|
|
68
|
+
# get http request body reader
|
|
69
|
+
multipart = k3httpmultipart.Multipart()
|
|
70
|
+
body_reader = multipart.make_body_reader(fields)
|
|
71
|
+
data = []
|
|
72
|
+
|
|
73
|
+
for body in body_reader:
|
|
74
|
+
data.append(body)
|
|
75
|
+
|
|
76
|
+
print(''.join(data))
|
|
77
|
+
|
|
78
|
+
#output:
|
|
79
|
+
#--FormBoundaryrGKCBY7
|
|
80
|
+
#Content-Disposition: form-data; name=aaa
|
|
81
|
+
#
|
|
82
|
+
#abcde
|
|
83
|
+
#--FormBoundaryrGKCBY7
|
|
84
|
+
#Content-Disposition: form-data; name=bbb; filename=abc.txt
|
|
85
|
+
#Content-Type: text/plain
|
|
86
|
+
#
|
|
87
|
+
#123456789
|
|
88
|
+
#--FormBoundaryrGKCBY7--
|
|
89
|
+
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
# Author
|
|
93
|
+
|
|
94
|
+
Zhang Yanpo (张炎泼) <drdr.xp@gmail.com>
|
|
95
|
+
|
|
96
|
+
# Copyright and License
|
|
97
|
+
|
|
98
|
+
The MIT License (MIT)
|
|
99
|
+
|
|
100
|
+
Copyright (c) 2015 Zhang Yanpo (张炎泼) <drdr.xp@gmail.com>
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
[pykit3]: https://github.com/pykit3
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from importlib.metadata import version
|
|
2
|
+
|
|
3
|
+
__version__ = version("k3httpmultipart")
|
|
4
|
+
|
|
5
|
+
from .multipart import (
|
|
6
|
+
Multipart,
|
|
7
|
+
InvalidArgumentTypeError,
|
|
8
|
+
MultipartError,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
__all__ = [
|
|
12
|
+
"Multipart",
|
|
13
|
+
"InvalidArgumentTypeError",
|
|
14
|
+
"MultipartError",
|
|
15
|
+
]
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: k3httpmultipart
|
|
3
|
+
Version: 0.1.2
|
|
4
|
+
Summary: HTTP multipart form data encoder
|
|
5
|
+
Author-email: Zhang Yanpo <drdr.xp@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/pykit3/k3httpmultipart
|
|
8
|
+
Project-URL: Documentation, https://k3httpmultipart.readthedocs.io
|
|
9
|
+
Keywords: http,multipart,form-data,upload
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Requires-Python: >=3.9
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: k3mime
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
25
|
+
Requires-Dist: ruff; extra == "dev"
|
|
26
|
+
Requires-Dist: coverage; extra == "dev"
|
|
27
|
+
Requires-Dist: k3fs; extra == "dev"
|
|
28
|
+
Provides-Extra: publish
|
|
29
|
+
Requires-Dist: build; extra == "publish"
|
|
30
|
+
Requires-Dist: twine; extra == "publish"
|
|
31
|
+
Requires-Dist: pk3; extra == "publish"
|
|
32
|
+
Provides-Extra: docs
|
|
33
|
+
Requires-Dist: mkdocs>=1.5; extra == "docs"
|
|
34
|
+
Requires-Dist: mkdocs-material>=9.0; extra == "docs"
|
|
35
|
+
Requires-Dist: mkdocstrings[python]>=0.24; extra == "docs"
|
|
36
|
+
Dynamic: license-file
|
|
37
|
+
|
|
38
|
+
# k3httpmultipart
|
|
39
|
+
|
|
40
|
+
[](https://github.com/pykit3/k3httpmultipart/actions/workflows/python-package.yml)
|
|
41
|
+
[](https://travis-ci.com/pykit3/k3httpmultipart)
|
|
42
|
+
[](https://k3httpmultipart.readthedocs.io/en/stable/?badge=stable)
|
|
43
|
+
[](https://pypi.org/project/k3httpmultipart)
|
|
44
|
+
|
|
45
|
+
This module provides some util methods to get multipart headers and body.
|
|
46
|
+
|
|
47
|
+
k3httpmultipart is a component of [pykit3] project: a python3 toolkit set.
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
# Name
|
|
51
|
+
|
|
52
|
+
k3httpmultipart
|
|
53
|
+
|
|
54
|
+
# Status
|
|
55
|
+
|
|
56
|
+
The library is considered production ready.
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
# Install
|
|
62
|
+
|
|
63
|
+
```
|
|
64
|
+
pip install k3httpmultipart
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
# Synopsis
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
|
|
71
|
+
import os
|
|
72
|
+
|
|
73
|
+
import k3httpmultipart
|
|
74
|
+
import k3fs
|
|
75
|
+
|
|
76
|
+
# http request headers
|
|
77
|
+
headers = {'Content-Length': 1200}
|
|
78
|
+
|
|
79
|
+
# http request fields
|
|
80
|
+
file_path = '/tmp/abc.txt'
|
|
81
|
+
k3fs.fwrite(file_path, '123456789')
|
|
82
|
+
fields = [
|
|
83
|
+
{
|
|
84
|
+
'name': 'aaa',
|
|
85
|
+
'value': 'abcde',
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
'name': 'bbb',
|
|
89
|
+
'value': [open(file_path), os.path.getsize(file_path), 'abc.txt']
|
|
90
|
+
},
|
|
91
|
+
]
|
|
92
|
+
|
|
93
|
+
# get http request headers
|
|
94
|
+
multipart = k3httpmultipart.Multipart()
|
|
95
|
+
res_headers = multipart.make_headers(fields, headers=headers)
|
|
96
|
+
|
|
97
|
+
print(res_headers)
|
|
98
|
+
|
|
99
|
+
#output:
|
|
100
|
+
#{
|
|
101
|
+
# 'Content-Type': 'multipart/form-data; boundary=FormBoundaryrGKCBY7',
|
|
102
|
+
# 'Conetnt-Length': 1200,
|
|
103
|
+
#}
|
|
104
|
+
|
|
105
|
+
# get http request body reader
|
|
106
|
+
multipart = k3httpmultipart.Multipart()
|
|
107
|
+
body_reader = multipart.make_body_reader(fields)
|
|
108
|
+
data = []
|
|
109
|
+
|
|
110
|
+
for body in body_reader:
|
|
111
|
+
data.append(body)
|
|
112
|
+
|
|
113
|
+
print(''.join(data))
|
|
114
|
+
|
|
115
|
+
#output:
|
|
116
|
+
#--FormBoundaryrGKCBY7
|
|
117
|
+
#Content-Disposition: form-data; name=aaa
|
|
118
|
+
#
|
|
119
|
+
#abcde
|
|
120
|
+
#--FormBoundaryrGKCBY7
|
|
121
|
+
#Content-Disposition: form-data; name=bbb; filename=abc.txt
|
|
122
|
+
#Content-Type: text/plain
|
|
123
|
+
#
|
|
124
|
+
#123456789
|
|
125
|
+
#--FormBoundaryrGKCBY7--
|
|
126
|
+
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
# Author
|
|
130
|
+
|
|
131
|
+
Zhang Yanpo (张炎泼) <drdr.xp@gmail.com>
|
|
132
|
+
|
|
133
|
+
# Copyright and License
|
|
134
|
+
|
|
135
|
+
The MIT License (MIT)
|
|
136
|
+
|
|
137
|
+
Copyright (c) 2015 Zhang Yanpo (张炎泼) <drdr.xp@gmail.com>
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
[pykit3]: https://github.com/pykit3
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
__init__.py
|
|
4
|
+
multipart.py
|
|
5
|
+
pyproject.toml
|
|
6
|
+
synopsis.py
|
|
7
|
+
./__init__.py
|
|
8
|
+
./multipart.py
|
|
9
|
+
./synopsis.py
|
|
10
|
+
k3httpmultipart.egg-info/PKG-INFO
|
|
11
|
+
k3httpmultipart.egg-info/SOURCES.txt
|
|
12
|
+
k3httpmultipart.egg-info/dependency_links.txt
|
|
13
|
+
k3httpmultipart.egg-info/requires.txt
|
|
14
|
+
k3httpmultipart.egg-info/top_level.txt
|
|
15
|
+
test/test_doctest.py
|
|
16
|
+
test/test_k3httpmultipart.py
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import uuid
|
|
2
|
+
import copy
|
|
3
|
+
|
|
4
|
+
import k3mime
|
|
5
|
+
from collections.abc import Iterator
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class MultipartError(Exception):
|
|
9
|
+
# The base class of the other exceptions in this module. It is a subclass of `Exception`
|
|
10
|
+
pass
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class InvalidArgumentTypeError(MultipartError):
|
|
14
|
+
# A subclass of `MultipartError` Raise if the type of value is not a str or a list or the type of value[0] is not a string, string reader, file reader or file object
|
|
15
|
+
pass
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class Multipart(object):
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
:param block_size: It represents the size of each reading file
|
|
22
|
+
:param boundary: a placeholder that represents out specified delimiter
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
def __init__(self, block_size=1024 * 1024):
|
|
26
|
+
self.block_size = block_size
|
|
27
|
+
|
|
28
|
+
self.boundary = uuid.uuid4().hex
|
|
29
|
+
|
|
30
|
+
self.delimiter = "--{b}".format(b=self.boundary)
|
|
31
|
+
self.terminator = "--{b}--".format(b=self.boundary)
|
|
32
|
+
|
|
33
|
+
def make_headers(self, fields, headers=None):
|
|
34
|
+
"""
|
|
35
|
+
Return a header according to the fields and headers
|
|
36
|
+
|
|
37
|
+
:param fields: is a list of the dict, and each elements contains `name`, `value` and `headers`,
|
|
38
|
+
`headers` is an optional argument
|
|
39
|
+
- `name`:
|
|
40
|
+
It's a string that represents field's name
|
|
41
|
+
|
|
42
|
+
- `value`:
|
|
43
|
+
The value represents field's content. The type of value can be a string or a
|
|
44
|
+
list, string indicates that the field is a normal string, However, there are
|
|
45
|
+
three arguments of list: `content`, `size` and `file_name`
|
|
46
|
+
|
|
47
|
+
- `content`:
|
|
48
|
+
The type of `content` can be string, reader, file object
|
|
49
|
+
The string type refers to the user want to upload a string. It takes the
|
|
50
|
+
string as the field body
|
|
51
|
+
|
|
52
|
+
The reader type refers to a generator. To read the contents of generator as
|
|
53
|
+
the field body
|
|
54
|
+
|
|
55
|
+
The file object type refers to a file object, To read the contents of file
|
|
56
|
+
as the field body
|
|
57
|
+
|
|
58
|
+
- `size`
|
|
59
|
+
`size` refers to the length of the content, When the type of `content` is a
|
|
60
|
+
string, size can be None
|
|
61
|
+
|
|
62
|
+
- `file_name`
|
|
63
|
+
`file_name` is an optional argument, if `file_name` is None, that indicates
|
|
64
|
+
that `content` is uploaded as a normal field, whereas, the field as a file
|
|
65
|
+
|
|
66
|
+
- `headers`:
|
|
67
|
+
a dict, key is the `field_header_name`, value is the `field_header_value`,
|
|
68
|
+
it contains user defined headers and the required headers, such as
|
|
69
|
+
'Content-Disposition' and 'Content-Type'
|
|
70
|
+
:param headers: a dict of http request headers, key is the `header_name`, value is the
|
|
71
|
+
`header_value`. It's a default argument and its default value is None
|
|
72
|
+
:return: a dict that represents the request headers
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
if headers is None:
|
|
76
|
+
headers = {}
|
|
77
|
+
else:
|
|
78
|
+
headers = copy.deepcopy(headers)
|
|
79
|
+
|
|
80
|
+
headers["Content-Type"] = "multipart/form-data; boundary={b}".format(b=self.boundary)
|
|
81
|
+
|
|
82
|
+
if "Content-Length" not in headers:
|
|
83
|
+
headers["Content-Length"] = self._get_body_size(fields)
|
|
84
|
+
|
|
85
|
+
return headers
|
|
86
|
+
|
|
87
|
+
def make_body_reader(self, fields):
|
|
88
|
+
"""
|
|
89
|
+
Return a body according to the fields
|
|
90
|
+
|
|
91
|
+
:param fields: refer to the explanation above fields
|
|
92
|
+
:return: a generator that represents the multipart request body
|
|
93
|
+
"""
|
|
94
|
+
|
|
95
|
+
for f in fields:
|
|
96
|
+
reader, fsize, headers = self._standardize_field(f["name"], f["value"], f.get("headers", {}))
|
|
97
|
+
|
|
98
|
+
yield self._get_field_header(headers)
|
|
99
|
+
|
|
100
|
+
for buf in reader:
|
|
101
|
+
yield buf
|
|
102
|
+
|
|
103
|
+
yield "\r\n"
|
|
104
|
+
|
|
105
|
+
yield self.terminator
|
|
106
|
+
|
|
107
|
+
def _standardize_field(self, name, value, headers):
|
|
108
|
+
if isinstance(value, str):
|
|
109
|
+
reader = self._make_str_reader(value)
|
|
110
|
+
fsize = len(value)
|
|
111
|
+
self._set_content_disposition(headers, name)
|
|
112
|
+
|
|
113
|
+
return reader, fsize, headers
|
|
114
|
+
|
|
115
|
+
elif isinstance(value, list):
|
|
116
|
+
reader, fsize, fname = self._standardize_value(value)
|
|
117
|
+
|
|
118
|
+
self._set_content_disposition(headers, name, fname)
|
|
119
|
+
if fname is not None:
|
|
120
|
+
headers.setdefault("Content-Type", k3mime.get_by_filename(fname))
|
|
121
|
+
|
|
122
|
+
return reader, fsize, headers
|
|
123
|
+
|
|
124
|
+
raise InvalidArgumentTypeError("type of value {x} is invalid".format(x=type(value)))
|
|
125
|
+
|
|
126
|
+
def _standardize_value(self, value):
|
|
127
|
+
reader, fsize, fname = (value + [None, None])[:3]
|
|
128
|
+
|
|
129
|
+
if isinstance(reader, type(open)):
|
|
130
|
+
reader = self._make_file_reader(reader)
|
|
131
|
+
|
|
132
|
+
elif isinstance(reader, str):
|
|
133
|
+
reader = self._make_str_reader(reader)
|
|
134
|
+
fsize = len(value[0])
|
|
135
|
+
|
|
136
|
+
elif isinstance(reader, Iterator):
|
|
137
|
+
pass
|
|
138
|
+
|
|
139
|
+
else:
|
|
140
|
+
raise InvalidArgumentTypeError("type of value[0] {x}is invalid".format(x=type(value[0])))
|
|
141
|
+
|
|
142
|
+
return reader, fsize, fname
|
|
143
|
+
|
|
144
|
+
def _get_field_size(self, field):
|
|
145
|
+
reader, fsize, headers = self._standardize_field(field["name"], field["value"], field.get("headers", {}))
|
|
146
|
+
|
|
147
|
+
field_headers = self._get_field_header(headers)
|
|
148
|
+
|
|
149
|
+
return len(field_headers) + fsize + len("\r\n")
|
|
150
|
+
|
|
151
|
+
def _get_body_size(self, fields):
|
|
152
|
+
body_size = 0
|
|
153
|
+
|
|
154
|
+
for f in fields:
|
|
155
|
+
body_size += self._get_field_size(f)
|
|
156
|
+
|
|
157
|
+
return body_size + len(self.terminator)
|
|
158
|
+
|
|
159
|
+
def _get_field_header(self, headers):
|
|
160
|
+
field_headers = [self.delimiter]
|
|
161
|
+
|
|
162
|
+
field_headers.append("Content-Disposition: " + headers.pop("Content-Disposition"))
|
|
163
|
+
|
|
164
|
+
for k, v in headers.items():
|
|
165
|
+
field_headers.append(k + ": " + v)
|
|
166
|
+
|
|
167
|
+
field_headers.extend([""] * 2)
|
|
168
|
+
|
|
169
|
+
return "\r\n".join(field_headers)
|
|
170
|
+
|
|
171
|
+
def _make_file_reader(self, file_object):
|
|
172
|
+
while True:
|
|
173
|
+
buf = file_object.read(self.block_size)
|
|
174
|
+
if buf == "":
|
|
175
|
+
break
|
|
176
|
+
yield buf
|
|
177
|
+
|
|
178
|
+
def _make_str_reader(self, data):
|
|
179
|
+
yield data
|
|
180
|
+
|
|
181
|
+
def _set_content_disposition(self, headers, name, fname=None):
|
|
182
|
+
if fname is None:
|
|
183
|
+
headers["Content-Disposition"] = "form-data; name={n}".format(n=name)
|
|
184
|
+
else:
|
|
185
|
+
headers["Content-Disposition"] = "form-data; name={n}; filename={fn}".format(n=name, fn=fname)
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "k3httpmultipart"
|
|
7
|
+
version = "0.1.2"
|
|
8
|
+
description = "HTTP multipart form data encoder"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = {text = "MIT"}
|
|
11
|
+
requires-python = ">=3.9"
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Zhang Yanpo", email = "drdr.xp@gmail.com" }
|
|
14
|
+
]
|
|
15
|
+
keywords = ["http", "multipart", "form-data", "upload"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 4 - Beta",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"Topic :: Software Development :: Libraries",
|
|
20
|
+
"License :: OSI Approved :: MIT License",
|
|
21
|
+
"Programming Language :: Python :: 3",
|
|
22
|
+
"Programming Language :: Python :: 3.9",
|
|
23
|
+
"Programming Language :: Python :: 3.10",
|
|
24
|
+
"Programming Language :: Python :: 3.11",
|
|
25
|
+
"Programming Language :: Python :: 3.12",
|
|
26
|
+
]
|
|
27
|
+
dependencies = [
|
|
28
|
+
"k3mime",
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
[project.urls]
|
|
32
|
+
Homepage = "https://github.com/pykit3/k3httpmultipart"
|
|
33
|
+
Documentation = "https://k3httpmultipart.readthedocs.io"
|
|
34
|
+
|
|
35
|
+
[project.optional-dependencies]
|
|
36
|
+
dev = [
|
|
37
|
+
"pytest>=7.0",
|
|
38
|
+
"ruff",
|
|
39
|
+
"coverage",
|
|
40
|
+
"k3fs",
|
|
41
|
+
]
|
|
42
|
+
publish = [
|
|
43
|
+
"build",
|
|
44
|
+
"twine",
|
|
45
|
+
"pk3",
|
|
46
|
+
]
|
|
47
|
+
docs = [
|
|
48
|
+
"mkdocs>=1.5",
|
|
49
|
+
"mkdocs-material>=9.0",
|
|
50
|
+
"mkdocstrings[python]>=0.24",
|
|
51
|
+
]
|
|
52
|
+
|
|
53
|
+
[tool.setuptools]
|
|
54
|
+
packages = ["k3httpmultipart"]
|
|
55
|
+
|
|
56
|
+
[tool.setuptools.package-dir]
|
|
57
|
+
k3httpmultipart = "."
|
|
58
|
+
|
|
59
|
+
[tool.ruff]
|
|
60
|
+
line-length = 120
|