本文整理汇总了Python中multidict.CIMultiDict.setdefault方法的典型用法代码示例。如果您正苦于以下问题:Python CIMultiDict.setdefault方法的具体用法?Python CIMultiDict.setdefault怎么用?Python CIMultiDict.setdefault使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类multidict.CIMultiDict
的用法示例。
在下文中一共展示了CIMultiDict.setdefault方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: from_raw_headers
# 需要导入模块: from multidict import CIMultiDict [as 别名]
# 或者: from multidict.CIMultiDict import setdefault [as 别名]
def from_raw_headers(cls, raw_headers):
headers = CIMultiDict()
decoded_headers = raw_headers.decode().split(utils.EOL)
for line in decoded_headers[1:]:
k, v = line.split(': ', 1)
if k in headers:
o = headers.setdefault(k, [])
if not isinstance(o, list):
o = [o]
o.append(v)
headers[k] = o
else:
headers[k] = v
m = FIRST_LINE_PATTERN['response']['regex'].match(decoded_headers[0])
if m:
d = m.groupdict()
return Response(status_code=int(d['status_code']),
status_message=d['status_message'],
headers=headers,
first_line=decoded_headers[0])
else:
m = FIRST_LINE_PATTERN['request']['regex'].match(decoded_headers[0])
if m:
d = m.groupdict()
cseq, _ = headers['CSeq'].split()
return Request(method=d['method'],
headers=headers,
cseq=int(cseq),
first_line=decoded_headers[0])
else:
LOG.debug(decoded_headers)
raise ValueError('Not a SIP message')
示例2: from_raw_message
# 需要导入模块: from multidict import CIMultiDict [as 别名]
# 或者: from multidict.CIMultiDict import setdefault [as 别名]
def from_raw_message(cls, raw_message):
lines = raw_message.split(utils.EOL)
first_line = lines.pop(0)
headers = CIMultiDict()
payload = ''
reading_headers = True
for line in lines:
if reading_headers:
if ': ' in line:
k, v = line.split(': ', 1)
if k in headers:
o = headers.setdefault(k, [])
if not isinstance(o, list):
o = [o]
o.append(v)
headers[k] = o
else:
headers[k] = v
else: # Finish to parse headers
reading_headers = False
else: # @todo: use content length to read payload
payload += line # reading payload
if payload == '':
payload = None
m = FIRST_LINE_PATTERN['response']['regex'].match(first_line)
if m:
d = m.groupdict()
return Response(status_code=int(d['status_code']),
status_message=d['status_message'],
headers=headers,
payload=payload)
else:
m = FIRST_LINE_PATTERN['request']['regex'].match(first_line)
if m:
d = m.groupdict()
return Request(method=d['method'],
headers=headers,
payload=payload)
else:
raise ValueError('Not a SIP message')
示例3: _ws_connect
# 需要导入模块: from multidict import CIMultiDict [as 别名]
# 或者: from multidict.CIMultiDict import setdefault [as 别名]
async def _ws_connect(
self,
url: StrOrURL, *,
method: str=hdrs.METH_GET,
protocols: Iterable[str]=(),
timeout: float=10.0,
receive_timeout: Optional[float]=None,
autoclose: bool=True,
autoping: bool=True,
heartbeat: Optional[float]=None,
auth: Optional[BasicAuth]=None,
origin: Optional[str]=None,
headers: Optional[LooseHeaders]=None,
proxy: Optional[StrOrURL]=None,
proxy_auth: Optional[BasicAuth]=None,
ssl: Union[SSLContext, bool, None, Fingerprint]=None,
proxy_headers: Optional[LooseHeaders]=None,
compress: int=0,
max_msg_size: int=4*1024*1024
) -> ClientWebSocketResponse:
if headers is None:
real_headers = CIMultiDict() # type: CIMultiDict[str]
else:
real_headers = CIMultiDict(headers)
default_headers = {
hdrs.UPGRADE: hdrs.WEBSOCKET,
hdrs.CONNECTION: hdrs.UPGRADE,
hdrs.SEC_WEBSOCKET_VERSION: '13',
}
for key, value in default_headers.items():
real_headers.setdefault(key, value)
sec_key = base64.b64encode(os.urandom(16))
real_headers[hdrs.SEC_WEBSOCKET_KEY] = sec_key.decode()
if protocols:
real_headers[hdrs.SEC_WEBSOCKET_PROTOCOL] = ','.join(protocols)
if origin is not None:
real_headers[hdrs.ORIGIN] = origin
if compress:
extstr = ws_ext_gen(compress=compress)
real_headers[hdrs.SEC_WEBSOCKET_EXTENSIONS] = extstr
if not isinstance(ssl, SSL_ALLOWED_TYPES):
raise TypeError("ssl should be SSLContext, bool, Fingerprint, "
"or None, got {!r} instead.".format(ssl))
# send request
resp = await self.request(method, url,
headers=real_headers,
read_until_eof=False,
auth=auth,
proxy=proxy,
proxy_auth=proxy_auth,
ssl=ssl,
proxy_headers=proxy_headers)
try:
# check handshake
if resp.status != 101:
raise WSServerHandshakeError(
resp.request_info,
resp.history,
message='Invalid response status',
status=resp.status,
headers=resp.headers)
if resp.headers.get(hdrs.UPGRADE, '').lower() != 'websocket':
raise WSServerHandshakeError(
resp.request_info,
resp.history,
message='Invalid upgrade header',
status=resp.status,
headers=resp.headers)
if resp.headers.get(hdrs.CONNECTION, '').lower() != 'upgrade':
raise WSServerHandshakeError(
resp.request_info,
resp.history,
message='Invalid connection header',
status=resp.status,
headers=resp.headers)
# key calculation
key = resp.headers.get(hdrs.SEC_WEBSOCKET_ACCEPT, '')
match = base64.b64encode(
hashlib.sha1(sec_key + WS_KEY).digest()).decode()
if key != match:
raise WSServerHandshakeError(
resp.request_info,
resp.history,
message='Invalid challenge response',
status=resp.status,
headers=resp.headers)
# websocket protocol
protocol = None
#.........这里部分代码省略.........