本文整理汇总了Python中multidict.MultiDict方法的典型用法代码示例。如果您正苦于以下问题:Python multidict.MultiDict方法的具体用法?Python multidict.MultiDict怎么用?Python multidict.MultiDict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类multidict
的用法示例。
在下文中一共展示了multidict.MultiDict方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_fields
# 需要导入模块: import multidict [as 别名]
# 或者: from multidict import MultiDict [as 别名]
def add_fields(self, *fields: Any) -> None:
to_add = list(fields)
while to_add:
rec = to_add.pop(0)
if isinstance(rec, io.IOBase):
k = guess_filename(rec, 'unknown')
self.add_field(k, rec) # type: ignore
elif isinstance(rec, (MultiDictProxy, MultiDict)):
to_add.extend(rec.items())
elif isinstance(rec, (list, tuple)) and len(rec) == 2:
k, fp = rec
self.add_field(k, fp) # type: ignore
else:
raise TypeError('Only io.IOBase, multidict and (name, file) '
'pairs allowed, use .add_field() for passing '
'more complex parameters, got {!r}'
.format(rec))
示例2: request
# 需要导入模块: import multidict [as 别名]
# 或者: from multidict import MultiDict [as 别名]
def request(
self,
name: str,
cardinality: Cardinality,
request_type: Type[_SendType],
reply_type: Type[_RecvType],
*,
timeout: Optional[float] = None,
deadline: Optional[Deadline] = None,
metadata: Optional[_MetadataLike] = None,
) -> Stream[_SendType, _RecvType]:
if timeout is not None and deadline is None:
deadline = Deadline.from_timeout(timeout)
elif timeout is not None and deadline is not None:
deadline = min(Deadline.from_timeout(timeout), deadline)
metadata = cast(_Metadata, MultiDict(metadata or ()))
return Stream(self, name, metadata, cardinality,
request_type, reply_type, codec=self._codec,
status_details_codec=self._status_details_codec,
dispatch=self.__dispatch__, deadline=deadline)
示例3: test_connection_error
# 需要导入模块: import multidict [as 别名]
# 或者: from multidict import MultiDict [as 别名]
def test_connection_error():
class BrokenChannel:
_calls_started = 0
def __connect__(self):
raise IOError('Intentionally broken connection')
stream = Stream(BrokenChannel(), '/foo/bar', MultiDict(),
Cardinality.UNARY_UNARY, DummyRequest, DummyReply,
codec=ProtoCodec(), status_details_codec=None,
dispatch=_DispatchChannelEvents())
with pytest.raises(IOError) as err:
async with stream:
await stream.send_request()
err.match('Intentionally broken connection')
示例4: with_query
# 需要导入模块: import multidict [as 别名]
# 或者: from multidict import MultiDict [as 别名]
def with_query(self, *args, **kwargs):
"""Return a new URL with query part replaced.
Accepts any Mapping (e.g. dict, multidict.MultiDict instances)
or str, autoencode the argument if needed.
A sequence of (key, value) pairs is supported as well.
It also can take an arbitrary number of keyword arguments.
Clear query if None is passed.
"""
# N.B. doesn't cleanup query/fragment
new_query = self._get_str_query(*args, **kwargs)
return URL(
self._val._replace(path=self._val.path, query=new_query), encoded=True
)
示例5: _prepare_headers
# 需要导入模块: import multidict [as 别名]
# 或者: from multidict import MultiDict [as 别名]
def _prepare_headers(
self,
headers: Optional[LooseHeaders]) -> 'CIMultiDict[str]':
""" Add default headers and transform it to CIMultiDict
"""
# Convert headers to MultiDict
result = CIMultiDict(self._default_headers)
if headers:
if not isinstance(headers, (MultiDictProxy, MultiDict)):
headers = CIMultiDict(headers)
added_names = set() # type: Set[str]
for key, value in headers.items():
if key in added_names:
result.add(key, value)
else:
result[key] = value
added_names.add(key)
return result
示例6: update_headers
# 需要导入模块: import multidict [as 别名]
# 或者: from multidict import MultiDict [as 别名]
def update_headers(self, headers: Optional[LooseHeaders]) -> None:
"""Update request headers."""
self.headers = CIMultiDict() # type: CIMultiDict[str]
# add host
netloc = cast(str, self.url.raw_host)
if helpers.is_ipv6_address(netloc):
netloc = '[{}]'.format(netloc)
if not self.url.is_default_port():
netloc += ':' + str(self.url.port)
self.headers[hdrs.HOST] = netloc
if headers:
if isinstance(headers, (dict, MultiDictProxy, MultiDict)):
headers = headers.items() # type: ignore
for key, value in headers:
# A special case for Host header
if key.lower() == 'host':
self.headers[key] = value
else:
self.headers.add(key, value)
示例7: ping
# 需要导入模块: import multidict [as 别名]
# 或者: from multidict import MultiDict [as 别名]
def ping(self, request):
"""
---
description: This end-point allow to test that service is up.
tags:
- Admin
produces:
- 'text/plain'
responses:
"202":
description: successful operation. Return "pong" text
"405":
description: invalid HTTP Method
"""
return web.Response(text="pong", headers=MultiDict(
{METADATA_SERVICE_HEADER: METADATA_SERVICE_VERSION}))
示例8: check_req
# 需要导入模块: import multidict [as 别名]
# 或者: from multidict import MultiDict [as 别名]
def check_req(self, mock_req, url='', query=None, data=None, headers=None):
if not query:
query = {}
if not data:
data = {}
if not headers:
headers = {}
if mock_req.method == 'GET':
query['no-cache'] = mock_req.query['no-cache']
self.assertEqual(mock_req.url, url)
self.assertEqual(mock_req.query.items(), MultiDict(query).items())
self.assertEqual(len(mock_req.headers), len(headers))
for key, val in headers.items():
self.assertIn(key, mock_req.headers)
if not val == '*':
self.assertEqual(mock_req.headers[key], val)
self.assertEqual(mock_req.data, data)
示例9: _get_multipart_params
# 需要导入模块: import multidict [as 别名]
# 或者: from multidict import MultiDict [as 别名]
def _get_multipart_params(request):
"""Extract a mapping of parts sent in a multipart request.
:rtype: MultiDict
"""
def get_part_name(part):
_, params = parse_content_disposition(part.headers.get(CONTENT_DISPOSITION))
return params.get("name")
def get_part_data(part):
if part.filename is None:
return part.text()
else:
return part.read(decode=True)
params = MultiDict()
async for part in await request.multipart():
params.add(get_part_name(part), await get_part_data(part))
return params
示例10: add_fields
# 需要导入模块: import multidict [as 别名]
# 或者: from multidict import MultiDict [as 别名]
def add_fields(self, *fields):
to_add = list(fields)
while to_add:
rec = to_add.pop(0)
if isinstance(rec, io.IOBase):
k = guess_filename(rec, 'unknown')
self.add_field(k, rec)
elif isinstance(rec, (MultiDictProxy, MultiDict)):
to_add.extend(rec.items())
elif isinstance(rec, (list, tuple)) and len(rec) == 2:
k, fp = rec
self.add_field(k, fp)
else:
raise TypeError('Only io.IOBase, multidict and (name, file) '
'pairs allowed, use .add_field() for passing '
'more complex parameters, got {!r}'
.format(rec))
示例11: _prepare_headers
# 需要导入模块: import multidict [as 别名]
# 或者: from multidict import MultiDict [as 别名]
def _prepare_headers(self, headers):
""" Add default headers and transform it to CIMultiDict
"""
# Convert headers to MultiDict
result = CIMultiDict(self._default_headers)
if headers:
if not isinstance(headers, (MultiDictProxy, MultiDict)):
headers = CIMultiDict(headers)
added_names = set()
for key, value in headers.items():
if key in added_names:
result.add(key, value)
else:
result[key] = value
added_names.add(key)
return result
示例12: download
# 需要导入模块: import multidict [as 别名]
# 或者: from multidict import MultiDict [as 别名]
def download(request: web.Request, params: Any, row: VFolderRow) -> web.Response:
folder_name = request.match_info['name']
access_key = request['keypair']['access_key']
files = params['files']
log.info('VFOLDER.DOWNLOAD (ak:{}, vf:{}, path:{})', access_key, folder_name, files[0])
folder_path = get_folder_hostpath(row, request.app)
for file in files:
try:
file_path = (folder_path / file).resolve()
file_path.relative_to(folder_path)
except ValueError:
raise InvalidAPIParameters('The requested path is out of the folder')
if not file_path.is_file():
raise InvalidAPIParameters(
f'You cannot download "{file}" because it is not a regular file.')
with aiohttp.MultipartWriter('mixed') as mpwriter:
total_payloads_length = 0
headers = multidict.MultiDict({'Content-Encoding': 'identity'})
try:
for file in files:
data = open(folder_path / file, 'rb')
payload = mpwriter.append(data, headers)
if payload.size is not None:
total_payloads_length += payload.size
except FileNotFoundError:
return web.Response(status=404, reason='File not found')
mpwriter._headers['X-TOTAL-PAYLOADS-LENGTH'] = str(total_payloads_length)
return web.Response(body=mpwriter, status=200)
示例13: download_files
# 需要导入模块: import multidict [as 别名]
# 或者: from multidict import MultiDict [as 别名]
def download_files(request: web.Request, params: Any) -> web.Response:
registry = request.app['registry']
session_name = request.match_info['session_name']
requester_access_key, owner_access_key = await get_access_key_scopes(request)
files = params.get('files')
log.info('DOWNLOAD_FILE (ak:{0}/{1}, s:{2}, path:{3!r})',
requester_access_key, owner_access_key, session_name,
files[0])
try:
assert len(files) <= 5, 'Too many files'
await registry.increment_session_usage(session_name, owner_access_key)
# TODO: Read all download file contents. Need to fix by using chuncking, etc.
results = await asyncio.gather(*map(
functools.partial(registry.download_file, session_name, owner_access_key),
files))
log.debug('file(s) inside container retrieved')
except asyncio.CancelledError:
raise
except BackendError:
log.exception('DOWNLOAD_FILE: exception')
raise
except (ValueError, FileNotFoundError):
raise InvalidAPIParameters('The file is not found.')
except Exception:
await request.app['error_monitor'].capture_exception(user=request['user']['uuid'])
log.exception('DOWNLOAD_FILE: unexpected error!')
raise InternalServerError
with aiohttp.MultipartWriter('mixed') as mpwriter:
headers = multidict.MultiDict({'Content-Encoding': 'identity'})
for tarbytes in results:
mpwriter.append(tarbytes, headers)
return web.Response(body=mpwriter, status=200)
示例14: test_multidict
# 需要导入模块: import multidict [as 别名]
# 或者: from multidict import MultiDict [as 别名]
def test_multidict():
d = MultiDict({'a': 1, 'b': 2})
d.add('b', 3)
v = pformat(d)
assert set(v.split('\n')) == {
"<MultiDict({",
" 'a': 1,",
" 'b': 2,",
" 'b': 3,",
"})>",
}
示例15: _send
# 需要导入模块: import multidict [as 别名]
# 或者: from multidict import MultiDict [as 别名]
def _send(self, request):
# Note: When using aiobotocore with dynamodb, requests fail on crc32
# checksum computation as soon as the response data reaches ~5KB.
# When AWS response is gzip compressed:
# 1. aiohttp is automatically decompressing the data
# (http://aiohttp.readthedocs.io/en/stable/client.html#binary-response-content)
# 2. botocore computes crc32 on the uncompressed data bytes and fails
# cause crc32 has been computed on the compressed data
# The following line forces aws not to use gzip compression,
# if there is a way to configure aiohttp not to perform decompression,
# we can remove the following line and take advantage of
# aws gzip compression.
# https://github.com/boto/botocore/issues/1255
url = request.url
headers = request.headers
data = request.body
headers['Accept-Encoding'] = 'identity'
headers_ = MultiDict(
(z[0], _text(z[1], encoding='utf-8')) for z in headers.items())
# botocore does this during the request so we do this here as well
# TODO: this should be part of the ClientSession, perhaps make wrapper
proxy = self.proxies.get(urlparse(url.lower()).scheme)
if isinstance(data, io.IOBase):
data = _IOBaseWrapper(data)
url = URL(url, encoded=True)
resp = await self.http_session.request(
request.method, url=url, headers=headers_, data=data, proxy=proxy)
# If we're not streaming, read the content so we can retry any timeout
# errors, see:
# https://github.com/boto/botocore/blob/develop/botocore/vendored/requests/sessions.py#L604
if not request.stream_output:
await resp.read()
return resp