• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python compat.to_bytes函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



在下文中一共展示了to_bytes函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: upload_binary_app

def upload_binary_app(environ, start_response):
    req = Request(environ)
    status = "200 OK"
    if req.method == "GET":
        body = to_bytes("""
<html>
    <head><title>form page</title></head>
    <body>
        <form method="POST" id="binary_upload_form"
              enctype="multipart/form-data">
            <input name="binary-file-field" type="file" />
            <input name="button" type="submit" value="binary" />
        </form>
    </body>
</html>
""")
    else:
        uploaded_files = req.POST.getall("binary-file-field")
        data = [str(n) for n in struct.unpack('255h', uploaded_files[0].value)]
        body = to_bytes("""
<html>
    <head><title>display page</title></head>
    <body>
        %s
    </body>
</html>
""" % join_bytes(',', data))
    headers = [
        ('Content-Type', 'text/html; charset=utf-8'),
        ('Content-Length', str(len(body)))]
    start_response(status, headers)
    return [body]
开发者ID:ailling,项目名称:webtest,代码行数:32,代码来源:test_file_upload.py


示例2: application_exc_info

def application_exc_info(environ, start_response):
    body = to_bytes('body stuff')
    headers = [
        ('Content-Type', 'text/plain; charset=utf-8'),
        ('Content-Length', str(len(body)))]
    start_response(to_bytes('200 OK'), headers, ('stuff',))
    return [body]
开发者ID:Asinox,项目名称:webtest,代码行数:7,代码来源:test_lint.py


示例3: test_writelines

 def test_writelines(self):
     fake_error = self.FakeError()
     error_wrapper = ErrorWrapper(fake_error)
     data = [to_bytes('a line'), to_bytes('another line')]
     error_wrapper.writelines(data)
     self.assertEqual(fake_error.written, data,
         "ErrorWrapper should call original writer")
开发者ID:Asinox,项目名称:webtest,代码行数:7,代码来源:test_lint.py


示例4: test_multiple_file_uploads_with_filename_and_contents

    def test_multiple_file_uploads_with_filename_and_contents(self):
        uploaded_file1_name = \
            os.path.join(os.path.dirname(__file__), "__init__.py")
        uploaded_file1_contents = open(uploaded_file1_name).read()
        if PY3:
            uploaded_file1_contents = to_bytes(uploaded_file1_contents)
        uploaded_file2_name = __file__
        uploaded_file2_contents = open(uploaded_file2_name).read()
        if PY3:
            uploaded_file2_contents = to_bytes(uploaded_file2_contents)

        app = webtest.TestApp(multiple_upload_file_app)
        res = app.get('/')
        self.assertEqual(res.status_int, 200)
        self.assertEqual(res.headers['content-type'], 'text/html; charset=utf-8')
        self.assertEqual(res.content_type, 'text/html')

        single_form = res.forms["file_upload_form"]
        single_form.set("file-field-1", (uploaded_file1_name, uploaded_file1_contents))
        single_form.set("file-field-2", (uploaded_file2_name, uploaded_file2_contents))
        display = single_form.submit("button")
        self.assertIn("<p>You selected '%s'</p>" % uploaded_file1_name, display, display)
        self.assertIn("<p>with contents: '%s'</p>" % to_string(uploaded_file1_contents), display, \
            display)
        self.assertIn("<p>You selected '%s'</p>" % uploaded_file2_name, display, display)
        self.assertIn("<p>with contents: '%s'</p>" % to_string(uploaded_file2_contents), display, \
            display)
开发者ID:ailling,项目名称:webtest,代码行数:27,代码来源:test_file_upload.py


示例5: test_multiple_file_uploads_with_filename_and_contents

    def test_multiple_file_uploads_with_filename_and_contents(self):
        uploaded_file1_name = os.path.join(os.path.dirname(__file__),
                                           "__init__.py")
        uploaded_file1_contents = open(uploaded_file1_name).read()
        if PY3:
            uploaded_file1_contents = to_bytes(uploaded_file1_contents)
        uploaded_file2_name = __file__
        uploaded_file2_name = os.path.join(os.path.dirname(__file__), 'html',
                                           "404.html")
        uploaded_file2_contents = open(uploaded_file2_name).read()
        if PY3:
            uploaded_file2_contents = to_bytes(uploaded_file2_contents)

        app = webtest.TestApp(MultipleUploadFileApp())
        res = app.get('/')
        self.assertEqual(res.status_int, 200)
        self.assertEqual(res.headers['content-type'],
                         'text/html; charset=utf-8')
        self.assertEqual(res.content_type, 'text/html')

        single_form = res.forms["file_upload_form"]
        single_form.set("file-field-1",
                        (uploaded_file1_name, uploaded_file1_contents))
        single_form.set("file-field-2",
                        (uploaded_file2_name, uploaded_file2_contents))
        display = single_form.submit("button")
        self.assertFile(uploaded_file1_name, uploaded_file1_contents, display)
        self.assertFile(uploaded_file1_name, uploaded_file1_contents, display)
开发者ID:lyndsysimon,项目名称:webtest,代码行数:28,代码来源:test_forms.py


示例6: test_encode_multipart_content_type

    def test_encode_multipart_content_type(self):
        data = self.app.encode_multipart([], [("file", "data.txt", six.b("data"), "text/x-custom-mime-type")])
        self.assertIn(to_bytes("Content-Type: text/x-custom-mime-type"), data[-1])

        data = self.app.encode_multipart(
            [("file", webtest.Upload("data.txt", six.b("data"), "text/x-custom-mime-type"))], []
        )
        self.assertIn(to_bytes("Content-Type: text/x-custom-mime-type"), data[-1])
开发者ID:bdauvergne,项目名称:webtest,代码行数:8,代码来源:test_app.py


示例7: test_encode_multipart_content_type

    def test_encode_multipart_content_type(self):
        data = self.app.encode_multipart(
            [], [('file', 'data.txt', six.b('data'), 'text/x-custom-mime-type')])
        self.assertIn(to_bytes('Content-Type: text/x-custom-mime-type'), data[-1])

        data = self.app.encode_multipart(
            [('file', webtest.Upload('data.txt', six.b('data'),
                                     'text/x-custom-mime-type'))], [])
        self.assertIn(to_bytes('Content-Type: text/x-custom-mime-type'), data[-1])
开发者ID:dmlayton,项目名称:webtest,代码行数:9,代码来源:test_app.py


示例8: cookie_app

 def cookie_app(environ, start_response):
     req = Request(environ)
     status = to_bytes("200 OK")
     body = "Cookie."
     assert dict(req.cookies) == {"spam": "eggs"}
     assert environ["HTTP_COOKIE"] == "spam=eggs"
     headers = [("Content-Type", "text/html"), ("Content-Length", str(len(body)))]
     start_response(status, headers)
     return [to_bytes(body)]
开发者ID:bdauvergne,项目名称:webtest,代码行数:9,代码来源:test_app.py


示例9: cookie_app3

def cookie_app3(environ, start_response):
    status = to_bytes("200 OK")
    body = 'Cookie: %(HTTP_COOKIE)s' % environ
    headers = [
        ('Content-Type', 'text/html'),
        ('Content-Length', str(len(body))),
    ]
    start_response(status, headers)
    return [to_bytes(body)]
开发者ID:ailling,项目名称:webtest,代码行数:9,代码来源:test_cookie.py


示例10: cookie_app

 def cookie_app(environ, start_response):
     status = to_bytes("200 OK")
     body = ''
     headers = [
         ('Content-Type', 'text/html'),
         ('Content-Length', str(len(body))),
         ('Set-Cookie', 'spam=eggs; Expires=Tue, 21-Feb-2013 17:45:00 GMT;'),
     ]
     start_response(status, headers)
     return [to_bytes(body)]
开发者ID:arthru,项目名称:webtest,代码行数:10,代码来源:test_app.py


示例11: cookie_app

 def cookie_app(environ, start_response):
     status = to_bytes("200 OK")
     body = 'Cookie.'
     headers = [
         ('Content-Type', 'text/html'),
         ('Content-Length', str(len(body))),
         ('Set-Cookie',
          'spam=eggs; Domain=localhost;'),
     ]
     start_response(status, headers)
     return [to_bytes(body)]
开发者ID:g761007,项目名称:webtest,代码行数:11,代码来源:test_app.py


示例12: cookie_app2

def cookie_app2(environ, start_response):
    status = to_bytes("200 OK")
    body = ''
    headers = [
        ('Content-Type', 'text/html'),
        ('Content-Length', str(len(body))),
        ('Set-Cookie', 'spam=eggs'),
        ('Set-Cookie', 'foo="bar;baz"'),
    ]
    start_response(status, headers)
    return [to_bytes(body)]
开发者ID:ailling,项目名称:webtest,代码行数:11,代码来源:test_cookie.py


示例13: get_files_page

    def get_files_page(self, req):
        file_parts = []
        uploaded_files = [(k, v) for k, v in req.POST.items() if 'file' in k]
        uploaded_files = sorted(uploaded_files)
        for name, uploaded_file in uploaded_files:
            filename = to_bytes(uploaded_file.filename)
            value = to_bytes(uploaded_file.value, 'ascii')
            file_parts.append(b"""
        <p>You selected '""" + filename + b"""'</p>
        <p>with contents: '""" + value + b"""'</p>
""")
        return b''.join(file_parts)
开发者ID:kmike,项目名称:webtest,代码行数:12,代码来源:test_forms.py


示例14: test_encode_multipart

    def test_encode_multipart(self):
        data = self.app.encode_multipart([], [("file", "data.txt", six.b("data"))])
        self.assertIn(to_bytes("data.txt"), data[-1])

        data = self.app.encode_multipart([], [(six.b("file"), six.b("data.txt"), six.b("data"))])
        self.assertIn(to_bytes("data.txt"), data[-1])

        data = self.app.encode_multipart([("key", "value")], [])
        self.assertIn(to_bytes('name="key"'), data[-1])

        data = self.app.encode_multipart([(six.b("key"), six.b("value"))], [])
        self.assertIn(to_bytes('name="key"'), data[-1])
开发者ID:bdauvergne,项目名称:webtest,代码行数:12,代码来源:test_app.py


示例15: select_app_without_default

def select_app_without_default(environ, start_response):
    req = Request(environ)
    status = b"200 OK"
    if req.method == "GET":
        body = to_bytes(
            """
<html>
    <head><title>form page</title></head>
    <body>
        <form method="POST" id="single_select_form">
            <select id="single" name="single">
                <option value="4">Four</option>
                <option value="5">Five</option>
                <option value="6">Six</option>
                <option value="7">Seven</option>
            </select>
            <input name="button" type="submit" value="single">
        </form>
        <form method="POST" id="multiple_select_form">
            <select id="multiple" name="multiple" multiple="multiple">
                <option value="8">Eight</option>
                <option value="9">Nine</option>
                <option value="10">Ten</option>
                <option value="11">Eleven</option>
            </select>
            <input name="button" type="submit" value="multiple">
        </form>
    </body>
</html>
"""
        )
    else:
        select_type = req.POST.get("button")
        if select_type == "single":
            selection = req.POST.get("single")
        elif select_type == "multiple":
            selection = ", ".join(req.POST.getall("multiple"))
        body = to_bytes(
            """
<html>
    <head><title>display page</title></head>
    <body>
        <p>You submitted the %(select_type)s </p>
        <p>You selected %(selection)s</p>
    </body>
</html>
"""
            % dict(selection=selection, select_type=select_type)
        )

    headers = [("Content-Type", "text/html; charset=utf-8"), ("Content-Length", str(len(body)))]
    start_response(status, headers)
    return [body]
开发者ID:jollychang,项目名称:webtest,代码行数:53,代码来源:test_forms.py


示例16: multiple_upload_file_app

def multiple_upload_file_app(environ, start_response):
    req = Request(environ)
    status = "200 OK"
    if req.method == "GET":
        body = to_bytes(
"""
<html>
    <head><title>form page</title></head>
    <body>
        <form method="POST" id="file_upload_form"
              enctype="multipart/form-data">
            <input name="file-field-1" type="file" />
            <input name="file-field-2" type="file" />
            <input name="button" type="submit" value="single">
        </form>
    </body>
</html>
""")
    else:
        uploaded_file_1 = req.POST.get("file-field-1")
        uploaded_file_2 = req.POST.get("file-field-2")
        uploaded_files = [uploaded_file_1, uploaded_file_2]

        body_head = to_bytes(
"""
<html>
    <head><title>display page</title></head>
    <body>
""")

        file_parts = []
        for uploaded_file in uploaded_files:
            print (to_bytes(uploaded_file.filename), type(uploaded_file.value))
            file_parts.append(
"""
        <p>You selected '%(filename)s'</p>
        <p>with contents: '%(value)s'</p>
""" % dict(filename=to_string(uploaded_file.filename),
           value=to_string(uploaded_file.value)))

        body_foot = to_bytes(
"""    </body>
</html>
""")
        body = body_head + join_bytes("", file_parts) + body_foot
    headers = [
        ('Content-Type', 'text/html; charset=utf-8'),
        ('Content-Length', str(len(body)))]
    start_response(status, headers)
    return [body]
开发者ID:ailling,项目名称:webtest,代码行数:50,代码来源:test_file_upload.py


示例17: test_encode_multipart

    def test_encode_multipart(self):
        data = self.app.encode_multipart(
            [], [('file', 'data.txt', six.b('data'))])
        self.assertIn(to_bytes('data.txt'), data[-1])

        data = self.app.encode_multipart(
            [], [(six.b('file'), six.b('data.txt'), six.b('data'))])
        self.assertIn(to_bytes('data.txt'), data[-1])

        data = self.app.encode_multipart(
            [('key', 'value')], [])
        self.assertIn(to_bytes('name="key"'), data[-1])

        data = self.app.encode_multipart(
            [(six.b('key'), six.b('value'))], [])
        self.assertIn(to_bytes('name="key"'), data[-1])
开发者ID:g761007,项目名称:webtest,代码行数:16,代码来源:test_app.py


示例18: test_post_with_file_upload

    def test_post_with_file_upload(self):
        uploaded_file_name = 'test.txt'
        uploaded_file_contents = 'test content file upload'
        if PY3:
            uploaded_file_contents = to_bytes(uploaded_file_contents)

        deform_upload_file_app = get_submit_app('deform',
                                                deform_upload_fields_text)
        app = webtest.TestApp(deform_upload_file_app)
        display = app.post("/", OrderedDict([
            ('_charset_', ''),
            ('__formid__', 'deform'),
            ('title', 'testtitle'),
            ('__start__', 'fileupload:mapping'),
            ('fileupload', webtest.Upload(uploaded_file_name,
                                          uploaded_file_contents)),
            ('__end__', 'fileupload:mapping'),
            ('description', 'testdescription'),
            ('Submit', 'Submit')]))

        self.assertIn("""
_charset_:
__formid__:deform
title:testtitle
__start__:fileupload:mapping
fileupload:test.txt:test content file upload
__end__:fileupload:mapping
description:testdescription
Submit:Submit""".strip(), display, display)
开发者ID:g761007,项目名称:webtest,代码行数:29,代码来源:test_app.py


示例19: test_submit_with_file_upload

    def test_submit_with_file_upload(self):
        uploaded_file_name = 'test.txt'
        uploaded_file_contents = 'test content file upload'
        if PY3:
            uploaded_file_contents = to_bytes(uploaded_file_contents)

        deform_upload_file_app = get_submit_app('deform',
                                                deform_upload_fields_text)
        app = webtest.TestApp(deform_upload_file_app)
        res = app.get('/')
        self.assertEqual(res.status_int, 200)
        self.assertEqual(
            res.headers['content-type'], 'text/html; charset=utf-8')
        self.assertEqual(res.content_type, 'text/html')
        self.assertEqual(res.charset, 'utf-8')

        single_form = res.forms["deform"]
        single_form.set("title", "testtitle")
        single_form.set("fileupload",
                        (uploaded_file_name, uploaded_file_contents))
        single_form.set("description", "testdescription")
        display = single_form.submit("Submit")
        self.assertIn("""
_charset_:
__formid__:deform
title:testtitle
__start__:fileupload:mapping
fileupload:test.txt:test content file upload
__end__:fileupload:mapping
description:testdescription
Submit:Submit
""".strip(), display, display)
开发者ID:g761007,项目名称:webtest,代码行数:32,代码来源:test_app.py


示例20: input_app_without_default

def input_app_without_default(environ, start_response):
    Request(environ)
    status = b"200 OK"
    body = to_bytes(
"""
<html>
    <head><title>form page</title></head>
    <body>
        <form method="POST" id="text_input_form">
            <input name="foo" type="text">
            <input name="button" type="submit" value="text">
        </form>
        <form method="POST" id="radio_input_form">
            <input name="foo" type="radio" value="bar">
            <input name="foo" type="radio" value="baz">
            <input name="button" type="submit" value="radio">
        </form>
        <form method="POST" id="checkbox_input_form">
            <input name="foo" type="checkbox" value="bar">
            <input name="button" type="submit" value="text">
        </form>
    </body>
</html>
""")
    headers = [
        ('Content-Type', 'text/html'),
        ('Content-Length', str(len(body)))]
    start_response(status, headers)
    return [body]
开发者ID:arthru,项目名称:webtest,代码行数:29,代码来源:test_forms.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python utils.stringify函数代码示例发布时间:2022-05-26
下一篇:
Python webtest.TestRequest类代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap