本文整理汇总了Python中webtest.compat.to_bytes函数的典型用法代码示例。如果您正苦于以下问题:Python to_bytes函数的具体用法?Python to_bytes怎么用?Python to_bytes使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了to_bytes函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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]
示例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]
示例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")
示例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)
示例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)
示例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])
示例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])
示例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)]
示例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)]
示例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)]
示例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)]
示例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)]
示例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)
示例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])
示例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]