本文整理汇总了Python中werkzeug.Request.from_values方法的典型用法代码示例。如果您正苦于以下问题:Python Request.from_values方法的具体用法?Python Request.from_values怎么用?Python Request.from_values使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类werkzeug.Request
的用法示例。
在下文中一共展示了Request.from_values方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _send_with_auth
# 需要导入模块: from werkzeug import Request [as 别名]
# 或者: from werkzeug.Request import from_values [as 别名]
def _send_with_auth(values, secret_key, url):
"""Send dictionary of JSON serializable `values` as a POST body to `url`
along with `auth_token` that's generated from `secret_key` and `values`
scheduler.auth.create_token expects a JSON serializable payload, so we send
a dictionary. On the receiving end of the POST request, the Flask view will
have access to a werkzeug.datastructures.ImmutableMultiDict. The easiest
and most surefire way to ensure that the payload sent to create_token will
be consistent on both ends is to generate an ImmutableMultiDict using the
werkzeug.Request.
"""
data = urllib.urlencode(values)
# Simulate a Flask request because that is what will be unpacked when the
# request is received on the other side
request = Request.from_values(
content_length=len(data),
input_stream=StringIO(data),
content_type='application/x-www-form-urlencoded',
method='POST')
# Add the auth_token, re-encode, and send
values['auth_token'] = create_token(secret_key, dict(request.form))
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
return json.loads(response.read())
示例2: test_wrapper_support
# 需要导入模块: from werkzeug import Request [as 别名]
# 或者: from werkzeug.Request import from_values [as 别名]
def test_wrapper_support():
"""Securecookie wrapper integration"""
req = Request.from_values()
resp = Response()
c = SecureCookie.load_cookie(req, secret_key='foo')
assert c.new
c['foo'] = 42
assert c.secret_key == 'foo'
c.save_cookie(resp)
req = Request.from_values(headers={
'Cookie': 'session="%s"' % parse_cookie(resp.headers['set-cookie'])['session']
})
c2 = SecureCookie.load_cookie(req, secret_key='foo')
assert not c2.new
assert c2 == c
示例3: test_large_file
# 需要导入模块: from werkzeug import Request [as 别名]
# 或者: from werkzeug.Request import from_values [as 别名]
def test_large_file():
"""Test a largish file."""
data = "x" * (1024 * 600)
req = Request.from_values(data={"foo": (StringIO(data), "test.txt")}, method="POST")
# make sure we have a real file here, because we expect to be
# on the disk. > 1024 * 500
assert isinstance(req.files["foo"].stream, file)
示例4: test_nonstandard_line_endings
# 需要导入模块: from werkzeug import Request [as 别名]
# 或者: from werkzeug.Request import from_values [as 别名]
def test_nonstandard_line_endings():
"""Test nonstandard line endings of multipart form data"""
for nl in "\n", "\r", "\r\n":
data = nl.join(
(
"--foo",
"Content-Disposition: form-data; name=foo",
"",
"this is just bar",
"--foo",
"Content-Disposition: form-data; name=bar",
"",
"blafasel",
"--foo--",
)
)
req = Request.from_values(
input_stream=StringIO(data),
content_length=len(data),
content_type="multipart/form-data; " "boundary=foo",
method="POST",
)
print req.form
assert req.form["foo"] == "this is just bar"
assert req.form["bar"] == "blafasel"
示例5: test_large_file
# 需要导入模块: from werkzeug import Request [as 别名]
# 或者: from werkzeug.Request import from_values [as 别名]
def test_large_file():
"""Test a largish file."""
data = 'x' * (1024 * 600)
req = Request.from_values(data={'foo': (StringIO(data), 'test.txt')},
method='POST')
# make sure we have a real file here, because we expect to be
# on the disk. > 1024 * 500
assert isinstance(req.files['foo'].stream, file)
示例6: test_multipart_file_no_content_type
# 需要导入模块: from werkzeug import Request [as 别名]
# 或者: from werkzeug.Request import from_values [as 别名]
def test_multipart_file_no_content_type():
"""Chrome does not always provide a content type."""
data = (
'--foo\r\n'
'Content-Disposition: form-data; name="test"; filename="test.txt"\r\n\r\n'
'file contents\r\n--foo--'
)
data = Request.from_values(input_stream=StringIO(data),
content_length=len(data),
content_type='multipart/form-data; boundary=foo',
method='POST')
assert data.files['test'].filename == 'test.txt'
assert data.files['test'].read() == 'file contents'
示例7: test_extra_newline_multipart
# 需要导入模块: from werkzeug import Request [as 别名]
# 或者: from werkzeug.Request import from_values [as 别名]
def test_extra_newline_multipart():
"""Test for multipart uploads with extra newlines"""
# this test looks innocent but it was actually timeing out in
# the Werkzeug 0.5 release version (#394)
data = "\r\n\r\n--foo\r\n" 'Content-Disposition: form-data; name="foo"\r\n\r\n' "a string\r\n" "--foo--"
data = Request.from_values(
input_stream=StringIO(data),
content_length=len(data),
content_type="multipart/form-data; boundary=foo",
method="POST",
)
assert not data.files
assert data.form["foo"] == "a string"
示例8: test_multipart_file_no_content_type
# 需要导入模块: from werkzeug import Request [as 别名]
# 或者: from werkzeug.Request import from_values [as 别名]
def test_multipart_file_no_content_type():
"""Chrome does not always provide a content type."""
data = (
"--foo\r\n"
'Content-Disposition: form-data; name="test"; filename="test.txt"\r\n\r\n'
"file contents\r\n--foo--"
)
data = Request.from_values(
input_stream=StringIO(data),
content_length=len(data),
content_type="multipart/form-data; boundary=foo",
method="POST",
)
assert data.files["test"].filename == "test.txt"
assert data.files["test"].read() == "file contents"
示例9: test_multipart_headers
# 需要导入模块: from werkzeug import Request [as 别名]
# 或者: from werkzeug.Request import from_values [as 别名]
def test_multipart_headers():
"""Test access to multipart headers"""
data = ('--foo\r\n'
'Content-Disposition: form-data; name="foo"; filename="foo.txt"\r\n'
'X-Custom-Header: blah\r\n'
'Content-Type: text/plain; charset=utf-8\r\n\r\n'
'file contents, just the contents\r\n'
'--foo--')
req = Request.from_values(input_stream=StringIO(data),
content_length=len(data),
content_type='multipart/form-data; boundary=foo',
method='POST')
foo = req.files['foo']
assert foo.content_type == 'text/plain'
assert foo.headers['content-type'] == 'text/plain; charset=utf-8'
assert foo.headers['x-custom-header'] == 'blah'
示例10: test_end_of_file_multipart
# 需要导入模块: from werkzeug import Request [as 别名]
# 或者: from werkzeug.Request import from_values [as 别名]
def test_end_of_file_multipart():
"""Test for multipart files ending unexpectedly"""
# This test looks innocent but it was actually timeing out in
# the Werkzeug 0.5 release version (#394)
data = (
'--foo\r\n'
'Content-Disposition: form-data; name="test"; filename="test.txt"\r\n'
'Content-Type: text/plain\r\n\r\n'
'file contents and no end'
)
data = Request.from_values(input_stream=StringIO(data),
content_length=len(data),
content_type='multipart/form-data; boundary=foo',
method='POST')
assert not data.files
assert not data.form
示例11: test_multipart_headers
# 需要导入模块: from werkzeug import Request [as 别名]
# 或者: from werkzeug.Request import from_values [as 别名]
def test_multipart_headers():
"""Test access to multipart headers"""
data = (
"--foo\r\n"
'Content-Disposition: form-data; name="foo"; filename="foo.txt"\r\n'
"X-Custom-Header: blah\r\n"
"Content-Type: text/plain; charset=utf-8\r\n\r\n"
"file contents, just the contents\r\n"
"--foo--"
)
req = Request.from_values(
input_stream=StringIO(data),
content_length=len(data),
content_type="multipart/form-data; boundary=foo",
method="POST",
)
foo = req.files["foo"]
assert foo.content_type == "text/plain"
assert foo.headers["content-type"] == "text/plain; charset=utf-8"
assert foo.headers["x-custom-header"] == "blah"
示例12: test_wrapper_internals
# 需要导入模块: from werkzeug import Request [as 别名]
# 或者: from werkzeug.Request import from_values [as 别名]
def test_wrapper_internals():
"""Test internals of the wrappers"""
from werkzeug import Request
req = Request.from_values(data={'foo': 'bar'}, method='POST')
req._load_form_data()
assert req.form.to_dict() == {'foo': 'bar'}
# second call does not break
req._load_form_data()
assert req.form.to_dict() == {'foo': 'bar'}
# check reprs
assert repr(req) == "<Request 'http://localhost/' [POST]>"
resp = Response()
assert repr(resp) == '<Response 0 bytes [200 OK]>'
resp.data = 'Hello World!'
assert repr(resp) == '<Response 12 bytes [200 OK]>'
resp.response = iter(['Test'])
assert repr(resp) == '<Response streamed [200 OK]>'
# unicode data does not set content length
response = Response([u'Hällo Wörld'])
headers = response.get_wsgi_headers(create_environ())
assert 'Content-Length' not in headers
response = Response(['Hällo Wörld'])
headers = response.get_wsgi_headers(create_environ())
assert 'Content-Length' in headers
# check for internal warnings
print 'start'
filterwarnings('error', category=Warning)
response = Response()
environ = create_environ()
response.response = 'What the...?'
assert_raises(Warning, lambda: list(response.iter_encoded()))
assert_raises(Warning, lambda: list(response.get_app_iter(environ)))
response.direct_passthrough = True
assert_raises(Warning, lambda: list(response.iter_encoded()))
assert_raises(Warning, lambda: list(response.get_app_iter(environ)))
resetwarnings()
示例13: test_nonstandard_line_endings
# 需要导入模块: from werkzeug import Request [as 别名]
# 或者: from werkzeug.Request import from_values [as 别名]
def test_nonstandard_line_endings():
"""Test nonstandard line endings of multipart form data"""
for nl in '\n', '\r', '\r\n':
data = nl.join((
'--foo',
'Content-Disposition: form-data; name=foo',
'',
'this is just bar',
'--foo',
'Content-Disposition: form-data; name=bar',
'',
'blafasel',
'--foo--'
))
req = Request.from_values(input_stream=StringIO(data),
content_length=len(data),
content_type='multipart/form-data; '
'boundary=foo', method='POST')
print req.form
assert req.form['foo'] == 'this is just bar'
assert req.form['bar'] == 'blafasel'
示例14: test_limiting
# 需要导入模块: from werkzeug import Request [as 别名]
# 或者: from werkzeug.Request import from_values [as 别名]
def test_limiting():
"""Test the limiting features"""
data = 'foo=Hello+World&bar=baz'
req = Request.from_values(input_stream=StringIO(data),
content_length=len(data),
content_type='application/x-www-form-urlencoded',
method='POST')
req.max_content_length = 4
req = Request.from_values(input_stream=StringIO(data),
content_length=len(data),
content_type='application/x-www-form-urlencoded',
method='POST')
req.max_content_length = 400
assert req.form['foo'] == 'Hello World'
req = Request.from_values(input_stream=StringIO(data),
content_length=len(data),
content_type='application/x-www-form-urlencoded',
method='POST')
req.max_form_memory_size = 7
assert_raises(RequestEntityTooLarge, lambda: req.form['foo'])
req = Request.from_values(input_stream=StringIO(data),
content_length=len(data),
content_type='application/x-www-form-urlencoded',
method='POST')
req.max_form_memory_size = 400
assert req.form['foo'] == 'Hello World'
data = ('--foo\r\nContent-Disposition: form-field; name=foo\r\n\r\n'
'Hello World\r\n'
'--foo\r\nContent-Disposition: form-field; name=bar\r\n\r\n'
'bar=baz\r\n--foo--')
req = Request.from_values(input_stream=StringIO(data),
content_length=len(data),
content_type='multipart/form-data; boundary=foo',
method='POST')
req.max_content_length = 4
assert_raises(RequestEntityTooLarge, lambda: req.form['foo'])
req = Request.from_values(input_stream=StringIO(data),
content_length=len(data),
content_type='multipart/form-data; boundary=foo',
method='POST')
req.max_content_length = 400
assert req.form['foo'] == 'Hello World'
req = Request.from_values(input_stream=StringIO(data),
content_length=len(data),
content_type='multipart/form-data; boundary=foo',
method='POST')
req.max_form_memory_size = 7
assert_raises(RequestEntityTooLarge, lambda: req.form['foo'])
req = Request.from_values(input_stream=StringIO(data),
content_length=len(data),
content_type='multipart/form-data; boundary=foo',
method='POST')
req.max_form_memory_size = 400
assert req.form['foo'] == 'Hello World'
示例15: test_limiting
# 需要导入模块: from werkzeug import Request [as 别名]
# 或者: from werkzeug.Request import from_values [as 别名]
def test_limiting():
"""Test the limiting features"""
data = "foo=Hello+World&bar=baz"
req = Request.from_values(
input_stream=StringIO(data),
content_length=len(data),
content_type="application/x-www-form-urlencoded",
method="POST",
)
req.max_content_length = 400
assert req.form["foo"] == "Hello World"
req = Request.from_values(
input_stream=StringIO(data),
content_length=len(data),
content_type="application/x-www-form-urlencoded",
method="POST",
)
req.max_form_memory_size = 7
assert_raises(RequestEntityTooLarge, lambda: req.form["foo"])
req = Request.from_values(
input_stream=StringIO(data),
content_length=len(data),
content_type="application/x-www-form-urlencoded",
method="POST",
)
req.max_form_memory_size = 400
assert req.form["foo"] == "Hello World"
data = (
"--foo\r\nContent-Disposition: form-field; name=foo\r\n\r\n"
"Hello World\r\n"
"--foo\r\nContent-Disposition: form-field; name=bar\r\n\r\n"
"bar=baz\r\n--foo--"
)
req = Request.from_values(
input_stream=StringIO(data),
content_length=len(data),
content_type="multipart/form-data; boundary=foo",
method="POST",
)
req.max_content_length = 4
assert_raises(RequestEntityTooLarge, lambda: req.form["foo"])
req = Request.from_values(
input_stream=StringIO(data),
content_length=len(data),
content_type="multipart/form-data; boundary=foo",
method="POST",
)
req.max_content_length = 400
assert req.form["foo"] == "Hello World"
req = Request.from_values(
input_stream=StringIO(data),
content_length=len(data),
content_type="multipart/form-data; boundary=foo",
method="POST",
)
req.max_form_memory_size = 7
assert_raises(RequestEntityTooLarge, lambda: req.form["foo"])
req = Request.from_values(
input_stream=StringIO(data),
content_length=len(data),
content_type="multipart/form-data; boundary=foo",
method="POST",
)
req.max_form_memory_size = 400
assert req.form["foo"] == "Hello World"