-
Notifications
You must be signed in to change notification settings - Fork 2
/
MultiPartForm.py
53 lines (45 loc) · 1.76 KB
/
MultiPartForm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
"""Accumulate the data to be used when posting a form."""
__version__ = "1.0"
__author__ = "unknown"
# zmousm: taken from here and simplified:
# http://code.google.com/p/uthcode/source/browse/trunk/python/urllib2-binary-upload.py?spec=svn533&r=533
# there are various other recipes, or one could also use poster:
# http://atlee.ca/software/poster/index.html
# but this was the easiest solution at the time (2012-03-16)
import itertools
import mimetools
import mimetypes
class MultiPartForm(object):
def __init__(self):
self.form_fields = []
self.boundary = mimetools.choose_boundary()
return
def get_content_type(self):
return 'multipart/form-data; boundary=%s' % self.boundary
def add_field(self, name, value):
"""Add a simple field to the form data."""
self.form_fields.append((name, value))
return
def __str__(self):
"""Return a string representing the form data."""
# Build a list of lists, each containing "lines" of the
# request. Each part is separated by a boundary string.
# Once the list is built, return a string where each
# line is separated by '\r\n'.
parts = []
part_boundary = '--' + self.boundary
# Add the form fields
parts.extend(
[ part_boundary,
'Content-Disposition: form-data; name="%s"' % name,
'',
value,
]
for name, value in self.form_fields
)
# Flatten the list and add closing boundary marker,
# then return CR+LF separated data
flattened = list(itertools.chain(*parts))
flattened.append('--' + self.boundary + '--')
flattened.append('')
return '\r\n'.join(flattened)