本文整理汇总了Python中urllib3.fields.RequestField.make_multipart方法的典型用法代码示例。如果您正苦于以下问题:Python RequestField.make_multipart方法的具体用法?Python RequestField.make_multipart怎么用?Python RequestField.make_multipart使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib3.fields.RequestField
的用法示例。
在下文中一共展示了RequestField.make_multipart方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_make_multipart_empty_filename
# 需要导入模块: from urllib3.fields import RequestField [as 别名]
# 或者: from urllib3.fields.RequestField import make_multipart [as 别名]
def test_make_multipart_empty_filename(self):
field = RequestField('somename', 'data', '')
field.make_multipart(content_type='application/octet-stream')
assert (
field.render_headers() ==
'Content-Disposition: form-data; name="somename"; filename=""\r\n'
'Content-Type: application/octet-stream\r\n'
'\r\n')
示例2: test_make_multipart
# 需要导入模块: from urllib3.fields import RequestField [as 别名]
# 或者: from urllib3.fields.RequestField import make_multipart [as 别名]
def test_make_multipart(self):
field = RequestField('somename', 'data')
field.make_multipart(content_type='image/jpg', content_location='/test')
self.assertEqual(field.render_headers(),
'Content-Disposition: form-data; name="somename"\r\n'
'Content-Type: image/jpg\r\n'
'Content-Location: /test\r\n'
'\r\n')
示例3: _encode_files
# 需要导入模块: from urllib3.fields import RequestField [as 别名]
# 或者: from urllib3.fields.RequestField import make_multipart [as 别名]
def _encode_files(files, data):
"""Build the body for a multipart/form-data request.
Will successfully encode files when passed as a dict or a list of
2-tuples. Order is retained if data is a list of 2-tuples but arbitrary
if parameters are supplied as a dict.
"""
if (not files):
raise ValueError("Files must be provided.")
elif isinstance(data, basestring):
raise ValueError("Data must not be a string.")
new_fields = []
fields = to_key_val_list(data or {})
files = to_key_val_list(files or {})
for field, val in fields:
if isinstance(val, basestring) or not hasattr(val, '__iter__'):
val = [val]
for v in val:
if v is not None:
# Don't call str() on bytestrings: in Py3 it all goes wrong.
if not isinstance(v, bytes):
v = str(v)
new_fields.append(
(field.decode('utf-8') if isinstance(field, bytes) else field,
v.encode('utf-8') if isinstance(v, str) else v))
for (k, v) in files:
# support for explicit filename
ft = None
fh = None
if isinstance(v, (tuple, list)):
if len(v) == 2:
fn, fp = v
elif len(v) == 3:
fn, fp, ft = v
else:
fn, fp, ft, fh = v
else:
fn = guess_filename(v) or k
fp = v
if isinstance(fp, (str, bytes, bytearray)):
fdata = fp
else:
fdata = fp.read()
rf = RequestField(name=k, data=fdata,
filename=fn, headers=fh)
rf.make_multipart(content_type=ft)
new_fields.append(rf)
body, content_type = encode_multipart_formdata(new_fields)
return body, content_type
示例4: test_make_multipart
# 需要导入模块: from urllib3.fields import RequestField [as 别名]
# 或者: from urllib3.fields.RequestField import make_multipart [as 别名]
def test_make_multipart(self):
field = RequestField("somename", "data")
field.make_multipart(content_type="image/jpg", content_location="/test")
self.assertEqual(
field.render_headers(),
'Content-Disposition: form-data; name="somename"\r\n'
"Content-Type: image/jpg\r\n"
"Content-Location: /test\r\n"
"\r\n",
)
示例5: process_upload_items
# 需要导入模块: from urllib3.fields import RequestField [as 别名]
# 或者: from urllib3.fields.RequestField import make_multipart [as 别名]
def process_upload_items(items):
result = []
for key, val in items:
if isinstance(val, UploadContent):
headers = {"Content-Type": val.content_type}
field = RequestField(name=key, data=val.content, filename=val.filename, headers=headers)
field.make_multipart(content_type=val.content_type)
result.append(field)
elif isinstance(val, UploadFile):
data = open(val.path, "rb").read()
headers = {"Content-Type": val.content_type}
field = RequestField(name=key, data=data, filename=val.filename, headers=headers)
field.make_multipart(content_type=val.content_type)
result.append(field)
else:
result.append((key, val))
return result
示例6: encode_body
# 需要导入模块: from urllib3.fields import RequestField [as 别名]
# 或者: from urllib3.fields.RequestField import make_multipart [as 别名]
def encode_body(self):
new_fields = []
for field, val in self.form_fields:
if isinstance(val, basestring) or not hasattr(val, '__iter__'):
val = [val]
for v in val:
if v is not None:
# Don't call str() on bytestrings: in Py3 it all goes wrong.
if not isinstance(v, bytes):
v = str(v)
new_fields.append(
(field.decode('utf-8') if isinstance(field, bytes) else field,
v.encode('utf-8') if isinstance(v, str) else v))
if not self.files:
self.files = {}
for k, v in self.files.items():
# support for explicit filename
ft = None
fh = None
if isinstance(v, (tuple, list)):
if len(v) == 2:
fn, fp = v
elif len(v) == 3:
fn, fp, ft = v
else:
fn, fp, ft, fh = v
else:
fn = guess_filename(v) or k
fp = v
if isinstance(fp, (str, bytes, bytearray)):
fdata = fp
else:
fdata = fp.read()
rf = RequestField(name=k, data=fdata,
filename=fn, headers=fh)
rf.make_multipart(content_type=ft)
new_fields.append(rf)
body, content_type = encode_multipart_formdata(new_fields)
return body,content_type
示例7: encode_params
# 需要导入模块: from urllib3.fields import RequestField [as 别名]
# 或者: from urllib3.fields.RequestField import make_multipart [as 别名]
def encode_params(self, data=None, files=None, **kwargs):
"""
Build the body for a multipart/form-data request.
Will successfully encode files when passed as a dict or a list of
tuples. Order is retained if data is a list of tuples but arbitrary
if parameters are supplied as a dict.
The tuples may be string (filepath), 2-tuples (filename, fileobj), 3-tuples (filename, fileobj, contentype)
or 4-tuples (filename, fileobj, contentype, custom_headers).
"""
if isinstance(data, basestring):
raise ValueError("Data must not be a string.")
# optional args
boundary = kwargs.get("boundary", None)
output_str = kwargs.get("output_str", self.output_str)
new_fields = []
fields = to_key_val_list(data or {})
files = to_key_val_list(files or {})
for field, value in fields:
ctype = None
if isinstance(value, (tuple, list)) and len(value) == 2:
val, ctype = value
else:
val = value
if isinstance(val, basestring) or not hasattr(val, '__iter__'):
val = [val]
for v in val:
# Don't call str() on bytestrings: in Py3 it all goes wrong.
if not isinstance(v, bytes):
v = to_string(v, lang=output_str)
field = field.decode('utf-8') if isinstance(field, bytes) else field
v = v.encode('utf-8') if isinstance(v, str) else v
rf = RequestField(name=field, data=v)
rf.make_multipart(content_type=ctype)
new_fields.append(rf)
for (k, v) in files:
# support for explicit filename
ft = None
fh = None
if isinstance(v, (tuple, list)):
if len(v) == 2:
fn, fp = v
elif len(v) == 3:
fn, fp, ft = v
else:
fn, fp, ft, fh = v
else:
fn, fp = guess_filename_stream(v)
ft = guess_content_type(fn)
if isinstance(fp, (str, bytes, bytearray)):
fdata = fp
else:
fdata = fp.read()
rf = RequestField(name=k, data=fdata, filename=fn, headers=fh)
rf.make_multipart(content_type=ft)
new_fields.append(rf)
if boundary is None:
boundary = self.boundary
body, content_type = encode_multipart_formdata(new_fields, boundary=boundary)
return body, content_type