當前位置: 首頁>>代碼示例>>Python>>正文


Python fields.RequestField類代碼示例

本文整理匯總了Python中urllib3.fields.RequestField的典型用法代碼示例。如果您正苦於以下問題:Python RequestField類的具體用法?Python RequestField怎麽用?Python RequestField使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了RequestField類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_render_parts

 def test_render_parts(self):
     field = RequestField('somename', 'data')
     parts = field._render_parts({'name': 'value', 'filename': 'value'})
     assert 'name="value"' in parts
     assert 'filename="value"' in parts
     parts = field._render_parts([('name', 'value'), ('filename', 'value')])
     assert parts == 'name="value"; filename="value"'
開發者ID:NickMinnellaCS96,項目名稱:urllib3,代碼行數:7,代碼來源:test_fields.py

示例2: test_render_parts

 def test_render_parts(self):
     field = RequestField("somename", "data")
     parts = field._render_parts({"name": "value", "filename": "value"})
     self.assertTrue('name="value"' in parts)
     self.assertTrue('filename="value"' in parts)
     parts = field._render_parts([("name", "value"), ("filename", "value")])
     self.assertEqual(parts, 'name="value"; filename="value"')
開發者ID:Nnamso,項目名稱:fjord,代碼行數:7,代碼來源:test_fields.py

示例3: test_make_multipart_empty_filename

 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')
開發者ID:NickMinnellaCS96,項目名稱:urllib3,代碼行數:8,代碼來源:test_fields.py

示例4: test_make_multipart

 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')
開發者ID:AngelMarquez,項目名稱:urllib3,代碼行數:8,代碼來源:test_fields.py

示例5: _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

示例6: test_render_part_rfc2231

 def test_render_part_rfc2231(self):
     field = RequestField('somename', 'data')
     field.style = 'RFC2231'
     param = field._render_part('filename', u('name'))
     self.assertEqual(param, 'filename="name"')
     param = field._render_part('filename', u('n\u00e4me'))
     self.assertEqual(param, "filename*=utf-8''n%C3%A4me")
     param = field._render_part('filename', 'some"really\nbad\\name')
     self.assertEqual(param, u("filename*=utf-8''some%22really%0Abad%5Cname"))
開發者ID:msrajan,項目名稱:urllib3,代碼行數:9,代碼來源:test_fields.py

示例7: test_render_part_html5

 def test_render_part_html5(self):
     field = RequestField('somename', 'data')
     field.style = 'HTML5'
     param = field._render_part('filename', u('name'))
     self.assertEqual(param, 'filename="name"')
     param = field._render_part('filename', u('n\u00e4me'))
     self.assertEqual(param, u('filename="n\u00e4me"'))
     param = field._render_part('filename', 'some"really\nbad\\name')
     self.assertEqual(param, 'filename="some\\"really bad\\\\name"')
開發者ID:msrajan,項目名稱:urllib3,代碼行數:9,代碼來源:test_fields.py

示例8: test_make_multipart

 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",
     )
開發者ID:Nnamso,項目名稱:fjord,代碼行數:10,代碼來源:test_fields.py

示例9: 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

示例10: test_create

 def test_create(self):
     simple_field = RequestField("somename", "data")
     self.assertEqual(simple_field.render_headers(), "\r\n")
     filename_field = RequestField("somename", "data", filename="somefile.txt")
     self.assertEqual(filename_field.render_headers(), "\r\n")
     headers_field = RequestField("somename", "data", headers={"Content-Length": 4})
     self.assertEqual(headers_field.render_headers(), "Content-Length: 4\r\n\r\n")
開發者ID:Nnamso,項目名稱:fjord,代碼行數:7,代碼來源:test_fields.py

示例11: test_create

 def test_create(self):
   simple_field = RequestField('somename', 'data')
   self.assertEqual(simple_field.render_headers(), '\r\n')
   filename_field = RequestField('somename', 'data', filename='somefile.txt')
   self.assertEqual(filename_field.render_headers(), '\r\n')
   headers_field = RequestField('somename', 'data', headers={'Content-Length': 4})
   self.assertEqual(headers_field.render_headers(),
       'Content-Length: 4\r\n'
       '\r\n')
開發者ID:AngelMarquez,項目名稱:urllib3,代碼行數:9,代碼來源:test_fields.py

示例12: test_create

 def test_create(self):
     simple_field = RequestField('somename', 'data')
     assert simple_field.render_headers() == '\r\n'
     filename_field = RequestField('somename', 'data',
                                   filename='somefile.txt')
     assert filename_field.render_headers() == '\r\n'
     headers_field = RequestField('somename', 'data',
                                  headers={'Content-Length': 4})
     assert headers_field.render_headers() == 'Content-Length: 4\r\n\r\n'
開發者ID:NickMinnellaCS96,項目名稱:urllib3,代碼行數:9,代碼來源:test_fields.py

示例13: test_create

 def test_create(self):
     simple_field = RequestField('somename', 'data')
     self.assertEqual(simple_field.render_headers(),
         'Content-Disposition: form-data; name="somename"\r\n\r\n')
     filename_field = RequestField('somename', 'data',
                                   filename='somefile.txt')
     self.assertEqual(filename_field.render_headers(),
         'Content-Disposition: form-data; name="somename"; filename="somefile.txt"\r\n'
         'Content-Type: text/plain\r\n'
         '\r\n')
     headers_field = RequestField('somename', 'data',
                                  headers={'Content-Length': 4})
     self.assertEqual(headers_field.render_headers(),
         'Content-Disposition: form-data; name="somename"\r\n'
         'Content-Length: 4\r\n\r\n')
開發者ID:msrajan,項目名稱:urllib3,代碼行數:15,代碼來源:test_fields.py

示例14: process_upload_items

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
開發者ID:fangjintang1989,項目名稱:grab,代碼行數:17,代碼來源:urllib3.py

示例15: test_render_part

 def test_render_part(self):
     field = RequestField('somename', 'data')
     param = field._render_part('filename', u('n\u00e4me'))
     self.assertEqual(param, "filename*=utf-8''n%C3%A4me")
開發者ID:Altynai,項目名稱:urllib3,代碼行數:4,代碼來源:test_fields.py


注:本文中的urllib3.fields.RequestField類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。