本文整理汇总了Python中urllib3.filepost.encode_multipart_formdata函数的典型用法代码示例。如果您正苦于以下问题:Python encode_multipart_formdata函数的具体用法?Python encode_multipart_formdata怎么用?Python encode_multipart_formdata使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了encode_multipart_formdata函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: from_data
def from_data(self, image_data, filetype=None, fileext='png'):
# bing transcodes images with javascript, then submits base64-encoded jpeg data...
image_size = len(image_data) / 1024
image_data, (width, height) = convert_image(image_data)
image_data = base64.b64encode(image_data) # base64 encoded submission
image_data = (None, image_data, None)
form_multipart = OrderedDict([
('imgurl', ''),
('cbir', 'sbi'),
('imageBin', image_data),
])
form_urlencoded = OrderedDict([
('q', ''),
('view', 'detailv2'),
('iss', 'sbi'),
('FORM', 'IRSBIQ'),
# probably nobody cares about that, but fake it anyway
('sbifsz', u'%s x %s · %s kB · %s'.encode('utf-8') \
% (width, height, image_size, fileext.encode('utf-8'))),
('sbifnm', 'image.%s' % fileext), # our "filename"
('thw', width),
('thh', height),
# disable safe search (TODO: only for nfsw sub searches?)
#('adlt', 'off'), # doesn't work anymore...
])
qstring = '?' + urlencode(form_urlencoded)
body, content_type = encode_multipart_formdata(form_multipart, boundary=None)
headers = {
b'Accept-Language': b'en-US,en;q=0.5',
b'Content-Type': content_type,
b'DNT': b'1',
}
return Request(self.search_image_url + qstring, method='POST', cookies=self.cookies,
body=body, headers=headers, callback=self.parse_image)
开发者ID:nyov,项目名称:reddit_info_bot,代码行数:34,代码来源:search.py
示例2: test_field_encoding
def test_field_encoding(self):
fieldsets = [
[('k', 'v'), ('k2', 'v2')],
[('k', b'v'), (u('k2'), b'v2')],
[('k', b'v'), (u('k2'), 'v2')],
]
for fields in fieldsets:
encoded, content_type = encode_multipart_formdata(fields, boundary=BOUNDARY)
self.assertEqual(encoded,
b'--' + b(BOUNDARY) + b'\r\n'
b'Content-Disposition: form-data; name="k"\r\n'
b'Content-Type: text/plain\r\n'
b'\r\n'
b'v\r\n'
b'--' + b(BOUNDARY) + b'\r\n'
b'Content-Disposition: form-data; name="k2"\r\n'
b'Content-Type: text/plain\r\n'
b'\r\n'
b'v2\r\n'
b'--' + b(BOUNDARY) + b'--\r\n'
, fields)
self.assertEqual(content_type,
b'multipart/form-data; boundary=' + b(BOUNDARY))
开发者ID:DazWorrall,项目名称:urllib3,代码行数:26,代码来源:test_filepost.py
示例3: test_filename
def test_filename(self):
fields = [('k', ('somename', b'v'))]
encoded, content_type = encode_multipart_formdata(fields, boundary=BOUNDARY)
self.assertEqual(encoded,
b'--' + b(BOUNDARY) + b'\r\n'
b'Content-Disposition: form-data; name="k"; filename="somename"\r\n'
b'Content-Type: application/octet-stream\r\n'
b'\r\n'
b'v\r\n'
b'--' + b(BOUNDARY) + b'--\r\n'
)
self.assertEqual(content_type,
b'multipart/form-data; boundary=' + b(BOUNDARY))
def test_textplain(self):
fields = [('k', ('somefile.txt', b'v'))]
encoded, content_type = encode_multipart_formdata(fields, boundary=BOUNDARY)
self.assertEqual(encoded,
b'--' + b(BOUNDARY) + b'\r\n'
b'Content-Disposition: form-data; name="k"; filename="somefile.txt"\r\n'
b'Content-Type: text/plain\r\n'
b'\r\n'
b'v\r\n'
b'--' + b(BOUNDARY) + b'--\r\n'
)
self.assertEqual(content_type,
b'multipart/form-data; boundary=' + b(BOUNDARY))
开发者ID:Bluehorn,项目名称:urllib3,代码行数:34,代码来源:test_filepost.py
示例4: post
def post(self, path, data={}, multipart=False, **kwargs):
"""
Convenience wrapper for the ``http_client``.
:param multipart:
If True the given ``data`` is encoded "multipart/form-data" through
``urllib3``
If the value is a tuple of two elements, then the first element is
treated as the filename of the form-data section.
"""
body = None
headers = {}
if multipart:
body, content_type = encode_multipart_formdata(data)
headers["Content-Type"] = content_type
else:
body = urllib.urlencode(data, doseq=True)
if 'headers' in kwargs:
kwargs['headers'].update(headers)
return self.fetch(
path,
method="POST",
body=body,
headers=headers,
**kwargs
)
开发者ID:wking,项目名称:tornado_pyvows,代码行数:28,代码来源:context.py
示例5: from_url
def from_url(self, image_url):
# do not 'optimize' .gifv links for KD
if not image_url.endswith('.gifv'):
image_url = find_media_url(image_url, self.settings)
#form_urlencoded = OrderedDict([
# ('kdtoolver', 'b1'),
# ('q', image_url),
#])
#return FormRequest(self.search_url, method='GET', formdata=form_urlencoded)
# use POST, more in line with browser
form_multipart = OrderedDict([
('MAX_FILE_SIZE', '10485760'),
('image', ''),
('url', image_url),
('search', 'search'),
('nsfwfilter', 'off'),
('subreddit[pics]', 'off'),
('subreddit[funny]', 'off'),
('subreddit[wtf]', 'off'),
('subreddit[nsfw]', 'off'),
('subreddit[others]', 'off'),
('subreddit[all]', 'off'),
])
body, content_type = encode_multipart_formdata(form_multipart, boundary=None)
headers = {
b'Content-Type': content_type,
b'DNT': b'1',
}
return Request(self.search_image_url, method='POST', body=body, headers=headers)
开发者ID:nyov,项目名称:reddit_info_bot,代码行数:31,代码来源:search.py
示例6: _encode_files
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
开发者ID:GymWenFLL,项目名称:tpp_libs,代码行数:58,代码来源:models.py
示例7: test_input_datastructures
def test_input_datastructures(self):
fieldsets = [
dict(k='v', k2='v2'),
[('k', 'v'), ('k2', 'v2')],
]
for fields in fieldsets:
encoded, _ = encode_multipart_formdata(fields, boundary=BOUNDARY)
self.assertEqual(encoded.count(b(BOUNDARY)), 3)
开发者ID:msrajan,项目名称:urllib3,代码行数:9,代码来源:test_filepost.py
示例8: test_control_style
def test_control_style(self):
fields = [(u('n\u00e4me\u011b'), u('va\u0142u\u00ea'))]
encoded, content_type = encode_multipart_formdata(
fields, boundary=BOUNDARY, field_encoding_style = 'RFC2231')
self.assertEquals(encoded,
b'--' + b(BOUNDARY) + b'\r\n'
b"Content-Disposition: form-data; name*=utf-8''n%C3%A4me%C4%9B\r\n"
b'\r\n'
b'va\xc5\x82u\xc3\xaa\r\n'
b'--' + b(BOUNDARY) + b'--\r\n'
)
开发者ID:msrajan,项目名称:urllib3,代码行数:11,代码来源:test_filepost.py
示例9: test_control_encoding
def test_control_encoding(self):
fields = [(u('n\u00e4me\u011b'), u('va\u0142u\u00ea'))]
encoded, content_type = encode_multipart_formdata(
fields, boundary=BOUNDARY, form_data_encoding = 'iso-8859-1')
self.assertEquals(encoded,
b'--' + b(BOUNDARY) + b'\r\n'
b'Content-Disposition: form-data; name="n\xe4meě"\r\n'
b'\r\n'
b'vału\xea\r\n'
b'--' + b(BOUNDARY) + b'--\r\n'
)
开发者ID:msrajan,项目名称:urllib3,代码行数:11,代码来源:test_filepost.py
示例10: test_request_fields
def test_request_fields(self):
fields = [RequestField('k', b'v', filename='somefile.txt', headers={'Content-Type': 'image/jpeg'})]
encoded, content_type = encode_multipart_formdata(fields, boundary=BOUNDARY)
self.assertEqual(encoded,
b'--' + b(BOUNDARY) + b'\r\n'
b'Content-Type: image/jpeg\r\n'
b'\r\n'
b'v\r\n'
b'--' + b(BOUNDARY) + b'--\r\n'
)
开发者ID:ANKIT-KS,项目名称:fjord,代码行数:12,代码来源:test_filepost.py
示例11: test_explicit
def test_explicit(self):
fields = [('k', ('somefile.txt', b'v', 'image/jpeg'))]
encoded, content_type = encode_multipart_formdata(fields, boundary=BOUNDARY)
expected = (b'--' + b(BOUNDARY) + b'\r\n'
b'Content-Disposition: form-data; name="k"; filename="somefile.txt"\r\n'
b'Content-Type: image/jpeg\r\n'
b'\r\n'
b'v\r\n'
b'--' + b(BOUNDARY) + b'--\r\n')
assert encoded == expected
assert content_type == 'multipart/form-data; boundary=' + str(BOUNDARY)
开发者ID:NickMinnellaCS96,项目名称:urllib3,代码行数:14,代码来源:test_filepost.py
示例12: test_filename
def test_filename(self):
fields = [('k', ('somename', b'v'))]
encoded, content_type = encode_multipart_formdata(fields, boundary=BOUNDARY)
expected = (b'--' + b(BOUNDARY) + b'\r\n'
b'Content-Disposition: form-data; name="k"; filename="somename"\r\n'
b'Content-Type: application/octet-stream\r\n'
b'\r\n'
b'v\r\n'
b'--' + b(BOUNDARY) + b'--\r\n')
assert encoded == expected
assert content_type == 'multipart/form-data; boundary=' + str(BOUNDARY)
开发者ID:NickMinnellaCS96,项目名称:urllib3,代码行数:14,代码来源:test_filepost.py
示例13: _encode_files
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 abritrary
if parameters are supplied as a dict.
"""
if (not files) or isinstance(data, str):
return None
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:
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
if isinstance(v, (tuple, list)):
if len(v) == 2:
fn, fp = v
else:
fn, fp, ft = v
else:
fn = guess_filename(v) or k
fp = v
if isinstance(fp, str):
fp = StringIO(fp)
if isinstance(fp, bytes):
fp = BytesIO(fp)
if ft:
new_v = (fn, fp.read(), ft)
else:
new_v = (fn, fp.read())
new_fields.append((k, new_v))
body, content_type = encode_multipart_formdata(new_fields)
return body, content_type
开发者ID:palladius,项目名称:facebook-sdk,代码行数:49,代码来源:models.py
示例14: test_field_encoding
def test_field_encoding(self, fields):
encoded, content_type = encode_multipart_formdata(fields, boundary=BOUNDARY)
expected = (b'--' + b(BOUNDARY) + b'\r\n'
b'Content-Disposition: form-data; name="k"\r\n'
b'\r\n'
b'v\r\n'
b'--' + b(BOUNDARY) + b'\r\n'
b'Content-Disposition: form-data; name="k2"\r\n'
b'\r\n'
b'v2\r\n'
b'--' + b(BOUNDARY) + b'--\r\n')
assert encoded == expected
assert content_type == 'multipart/form-data; boundary=' + str(BOUNDARY)
开发者ID:NickMinnellaCS96,项目名称:urllib3,代码行数:15,代码来源:test_filepost.py
示例15: test_textplain
def test_textplain(self):
fields = [('k', ('somefile.txt', b'v'))]
encoded, content_type = encode_multipart_formdata(fields, boundary=BOUNDARY)
expected = (b'--' + b(BOUNDARY) + b'\r\n'
b'Content-Disposition: form-data; name="k"; filename="somefile.txt"\r\n'
b'Content-Type: text/plain\r\n'
b'\r\n'
b'v\r\n'
b'--' + b(BOUNDARY) + b'--\r\n')
self.assertEqual(encoded, expected)
self.assertEqual(content_type,
'multipart/form-data; boundary=' + str(BOUNDARY))
开发者ID:Lukasa,项目名称:urllib3,代码行数:15,代码来源:test_filepost.py
示例16: multipart
def multipart(name, data, content_type='image/jpeg'):
"""Encode data as multipart form
data will be encoded as form file field with name and filename
equal to `name`
returns tuple of (encoded body, content type)
"""
fields = {
name: (name, data, content_type)
}
encoded = filepost.encode_multipart_formdata(fields)
return encoded
开发者ID:wkevina,项目名称:garage,代码行数:16,代码来源:client.py
示例17: encode_body
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
开发者ID:pacoyang,项目名称:tornado-qiniu,代码行数:43,代码来源:tornadohttpclient.py
示例18: process_config
def process_config(self, grab):
req = Request(data=None)
try:
request_url = normalize_url(grab.config["url"])
except Exception as ex:
raise error.GrabInvalidUrl(u"%s: %s" % (six.text_type(ex), grab.config["url"]))
req.url = request_url
method = grab.detect_request_method()
req.method = make_str(method)
req.body_maxsize = grab.config["body_maxsize"]
if grab.config["nobody"]:
req.body_maxsize = 0
req.timeout = grab.config["timeout"]
req.connect_timeout = grab.config["connect_timeout"]
extra_headers = {}
# Body processing
if grab.config["body_inmemory"]:
pass
else:
if not grab.config["body_storage_dir"]:
raise GrabMisuseError("Option body_storage_dir is not defined")
file_, path_ = self.setup_body_file(
grab.config["body_storage_dir"],
grab.config["body_storage_filename"],
create_dir=grab.config["body_storage_create_dir"],
)
req._response_file = file_
req._response_path = path_
if grab.config["multipart_post"] is not None:
post_data = grab.config["multipart_post"]
if isinstance(post_data, six.binary_type):
pass
elif isinstance(post_data, six.text_type):
raise GrabMisuseError("Option multipart_post data" " does not accept unicode.")
else:
post_items = normalize_http_values(
grab.config["multipart_post"],
charset=grab.config["charset"],
ignore_classes=(UploadFile, UploadContent),
)
# if six.PY3:
post_items = decode_pairs(post_items, grab.config["charset"])
post_items = process_upload_items(post_items)
post_data, content_type = encode_multipart_formdata(post_items)
extra_headers["Content-Type"] = content_type
extra_headers["Content-Length"] = len(post_data)
req.data = post_data
elif grab.config["post"] is not None:
post_data = normalize_post_data(grab.config["post"], grab.config["charset"])
# py3 hack
# if six.PY3:
# post_data = smart_unicode(post_data,
# grab.config['charset'])
extra_headers["Content-Length"] = len(post_data)
req.data = post_data
if method in ("POST", "PUT"):
if grab.config["post"] is None and grab.config["multipart_post"] is None:
raise GrabMisuseError(
"Neither `post` or `multipart_post`" " options was specified for the %s" " request" % method
)
# Proxy
if grab.config["proxy"]:
req.proxy = grab.config["proxy"]
if grab.config["proxy_userpwd"]:
req.proxy_userpwd = grab.config["proxy_userpwd"]
if grab.config["proxy_type"]:
req.proxy_type = grab.config["proxy_type"]
else:
req.proxy_type = "http"
# User-Agent
if grab.config["user_agent"] is None:
if grab.config["user_agent_file"] is not None:
with open(grab.config["user_agent_file"]) as inf:
lines = inf.read().splitlines()
grab.config["user_agent"] = random.choice(lines)
else:
grab.config["user_agent"] = generate_user_agent()
extra_headers["User-Agent"] = grab.config["user_agent"]
# Headers
headers = extra_headers
headers.update(grab.config["common_headers"])
if grab.config["headers"]:
headers.update(grab.config["headers"])
req.headers = headers
# Cookies
#.........这里部分代码省略.........
开发者ID:fangjintang1989,项目名称:grab,代码行数:101,代码来源:urllib3.py
示例19: request
def request(self, method, url, query_params=None, headers=None,
body=None, post_params=None, _preload_content=True, _request_timeout=None):
"""
:param method: http request method
:param url: http request url
:param query_params: query parameters in the url
:param headers: http request headers
:param body: request json body, for `application/json`
:param post_params: request post parameters,
`application/x-www-form-urlencoded`
and `multipart/form-data`
:param _preload_content: this is a non-applicable field for the AiohttpClient.
:param _request_timeout: timeout setting for this request. If one number provided, it will be total request
timeout. It can also be a pair (tuple) of (connection, read) timeouts.
"""
method = method.upper()
assert method in ['GET', 'HEAD', 'DELETE', 'POST', 'PUT', 'PATCH', 'OPTIONS']
if post_params and body:
raise ValueError(
"body parameter cannot be used with post_params parameter."
)
request = HTTPRequest(url)
request.ssl_context = self.ssl_context
request.proxy_host = self.proxy_host
request.proxy_port = self.proxy_port
request.method = method
if headers:
request.headers = headers
if 'Content-Type' not in headers:
request.headers['Content-Type'] = 'application/json'
request.request_timeout = _request_timeout or 5 * 60
post_params = post_params or {}
if query_params:
request.url += '?' + urlencode(query_params)
# For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE`
if method in ['POST', 'PUT', 'PATCH', 'OPTIONS', 'DELETE']:
if re.search('json', headers['Content-Type'], re.IGNORECASE):
if body:
body = json.dumps(body)
request.body = body
elif headers['Content-Type'] == 'application/x-www-form-urlencoded':
request.body = urlencode(post_params)
# TODO: transform to multipart form
elif headers['Content-Type'] == 'multipart/form-data':
request.body = encode_multipart_formdata(post_params)
# Pass a `bytes` parameter directly in the body to support
# other content types than Json when `body` argument is provided
# in serialized form
elif isinstance(body, bytes):
request.body = body
else:
# Cannot generate the request from given parameters
msg = """Cannot prepare a request message for provided arguments.
Please check that your arguments match declared content type."""
raise ApiException(status=0, reason=msg)
r = yield self.pool_manager.fetch(request)
r = RESTResponse(r, r.body)
# log response body
logger.debug("response body: %s", r.data)
if not 200 <= r.status <= 299:
raise ApiException(http_resp=r)
return r
开发者ID:markcuk,项目名称:swagger-codegen,代码行数:72,代码来源:rest.py
示例20: test_input_datastructures
def test_input_datastructures(self, fields):
encoded, _ = encode_multipart_formdata(fields, boundary=BOUNDARY)
assert encoded.count(b(BOUNDARY)) == 3
开发者ID:NickMinnellaCS96,项目名称:urllib3,代码行数:3,代码来源:test_filepost.py
注:本文中的urllib3.filepost.encode_multipart_formdata函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论