当前位置: 首页>>代码示例>>Python>>正文


Python filepost.encode_multipart_formdata函数代码示例

本文整理汇总了Python中urllib3.filepost.encode_multipart_formdata函数的典型用法代码示例。如果您正苦于以下问题:Python encode_multipart_formdata函数的具体用法?Python encode_multipart_formdata怎么用?Python encode_multipart_formdata使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了encode_multipart_formdata函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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


注:本文中的urllib3.filepost.encode_multipart_formdata函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。