本文整理汇总了Python中requests.structures.CaseInsensitiveDict类的典型用法代码示例。如果您正苦于以下问题:Python CaseInsensitiveDict类的具体用法?Python CaseInsensitiveDict怎么用?Python CaseInsensitiveDict使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CaseInsensitiveDict类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: assert_request_equal
def assert_request_equal(self, expected, real_request):
method, path = expected[:2]
if urlparse(path).scheme:
match_path = real_request['full_path']
else:
match_path = real_request['path']
self.assertEqual((method, path), (real_request['method'],
match_path))
if len(expected) > 2:
body = expected[2]
real_request['expected'] = body
err_msg = 'Body mismatch for %(method)s %(path)s, ' \
'expected %(expected)r, and got %(body)r' % real_request
self.orig_assertEqual(body, real_request['body'], err_msg)
if len(expected) > 3:
headers = CaseInsensitiveDict(expected[3])
for key, value in headers.items():
real_request['key'] = key
real_request['expected_value'] = value
real_request['value'] = real_request['headers'].get(key)
err_msg = (
'Header mismatch on %(key)r, '
'expected %(expected_value)r and got %(value)r '
'for %(method)s %(path)s %(headers)r' % real_request)
self.orig_assertEqual(value, real_request['value'],
err_msg)
real_request['extra_headers'] = dict(
(key, value) for key, value in real_request['headers'].items()
if key not in headers)
if real_request['extra_headers']:
self.fail('Received unexpected headers for %(method)s '
'%(path)s, got %(extra_headers)r' % real_request)
示例2: _check_creds
def _check_creds(self, creds):
d = CaseInsensitiveDict()
if isinstance(creds, dict):
d.update(creds)
elif isinstance(creds, basestring):
if os.path.exists(creds):
creds = file(creds, "r").read()
for line in creds.splitlines():
if ":" in line:
k, v = line.split(":", 1)
d[k.strip()] = v.strip()
else:
raise TypeError("unsupported type for credentials data")
if "companyId" not in d and "CID" in d:
d["companyId"] = d["CID"]
if "companyId" in d and not "psk" in d:
raise ValueError("psk is required when companyId is provided")
elif "psk" in d and not "companyId" in d:
raise ValueError("companyId is required when psk is provided")
elif "companyId" in d and "psk" in d:
return {"companyId": int(d["companyId"]), "psk": str(d["psk"])}
elif "loginSessionId" in d and not "profileId" in d:
raise ValueError("profileId is required when loginSessionId is " "provided")
elif "profileId" in d and not "loginSessionId" in d:
raise ValueError("loginSessionId is required when profileId is " "provided")
elif "loginSessionId" in d and "profileId" in d:
return {"loginSessionId": str(d["loginSessionId"]), "profileId": int(d["profileId"])}
else:
raise ValueError("either companyId+psk or " "loginSessionId+profileId must be provided")
示例3: send_document
def send_document(url, data, timeout=10, *args, **kwargs):
"""Helper method to send a document via POST.
Additional ``*args`` and ``**kwargs`` will be passed on to ``requests.post``.
:arg url: Full url to send to, including protocol
:arg data: POST data to send (dict)
:arg timeout: Seconds to wait for response (defaults to 10)
:returns: Tuple of status code (int or None) and error (exception class instance or None)
"""
logger.debug("send_document: url=%s, data=%s, timeout=%s", url, data, timeout)
headers = CaseInsensitiveDict({
'User-Agent': USER_AGENT,
})
if "headers" in kwargs:
# Update from kwargs
headers.update(kwargs.get("headers"))
kwargs.update({
"data": data, "timeout": timeout, "headers": headers
})
try:
response = requests.post(url, *args, **kwargs)
logger.debug("send_document: response status code %s", response.status_code)
return response.status_code, None
except RequestException as ex:
logger.debug("send_document: exception %s", ex)
return None, ex
示例4: assert_request_equal
def assert_request_equal(self, expected, real_request):
method, path = expected[:2]
if urlparse(path).scheme:
match_path = real_request["full_path"]
else:
match_path = real_request["path"]
self.assertEqual((method, path), (real_request["method"], match_path))
if len(expected) > 2:
body = expected[2]
real_request["expected"] = body
err_msg = "Body mismatch for %(method)s %(path)s, " "expected %(expected)r, and got %(body)r" % real_request
self.orig_assertEqual(body, real_request["body"], err_msg)
if len(expected) > 3:
headers = CaseInsensitiveDict(expected[3])
for key, value in headers.items():
real_request["key"] = key
real_request["expected_value"] = value
real_request["value"] = real_request["headers"].get(key)
err_msg = (
"Header mismatch on %(key)r, "
"expected %(expected_value)r and got %(value)r "
"for %(method)s %(path)s %(headers)r" % real_request
)
self.orig_assertEqual(value, real_request["value"], err_msg)
real_request["extra_headers"] = dict(
(key, value) for key, value in real_request["headers"].items() if key not in headers
)
if real_request["extra_headers"]:
self.fail(
"Received unexpected headers for %(method)s " "%(path)s, got %(extra_headers)r" % real_request
)
示例5: DDWRT
class DDWRT(Router):
def __init__(self, conf, hostnames):
self.hostnames = CaseInsensitiveDict()
self.hostnames.update(hostnames)
self.conf = conf
self.auth = self.conf.auth()
def clients(self):
""" Receives all currently logged in users in a wifi network.
:rtype : list
:return: Returns a list of dicts, containing the following keys: mac, ipv4, seen, hostname
"""
clients = self._get_clients_raw()
clients_json = []
for client in clients:
client_hostname_from_router = client[0]
client_ipv4 = client[1].strip()
client_mac = client[2].strip().upper()
client_hostname = self.hostnames.get(client_mac, client_hostname_from_router).strip()
client_connections = int(client[3].strip())
# Clients with less than 20 connections are considered offline
if client_connections < 20:
continue
clients_json.append({
'mac': client_mac,
'ipv4': client_ipv4,
'seen': int(time.time()),
'hostname': client_hostname,
})
logger.debug('The router got us {} clients.'.format(len(clients_json)))
logger.debug(str(clients_json))
return clients_json
def _get_clients_raw(self):
info_page = self.conf.internal()
response = requests.get(info_page, auth=self.auth)
logger.info('Got response from router with code {}.'.format(response.status_code))
return DDWRT._convert_to_clients(response.text) or []
@staticmethod
def _convert_to_clients(router_info_all):
# Split router info in lines and filter empty info
router_info_lines = filter(None, router_info_all.split("\n"))
# Get key / value of router info
router_info_items = dict()
for item in router_info_lines:
key, value = item[1:-1].split("::") # Remove curly braces and split
router_info_items[key.strip()] = value.strip()
# Get client info as a list
arp_table = utils.groupn(router_info_items['arp_table'].replace("'", "").split(","), 4)
dhcp_leases = utils.groupn(router_info_items['dhcp_leases'].replace("'", "").split(","), 5)
return arp_table if (len(arp_table) > 0) else []
示例6: prepare_response
def prepare_response(self, cached):
"""Verify our vary headers match and construct a real urllib3
HTTPResponse object.
"""
# Special case the '*' Vary value as it means we cannot actually
# determine if the cached response is suitable for this request.
if "*" in cached.get("vary", {}):
return
body_raw = cached["response"].pop("body")
headers = CaseInsensitiveDict(data=cached['response']['headers'])
if headers.get('transfer-encoding', '') == 'chunked':
headers.pop('transfer-encoding')
cached['response']['headers'] = headers
try:
body = io.BytesIO(body_raw)
except TypeError:
# This can happen if cachecontrol serialized to v1 format (pickle)
# using Python 2. A Python 2 str(byte string) will be unpickled as
# a Python 3 str (unicode string), which will cause the above to
# fail with:
#
# TypeError: 'str' does not support the buffer interface
body = io.BytesIO(body_raw.encode('utf8'))
return HTTPResponse(
body=body,
preload_content=False,
**cached["response"]
)
示例7: Attachment
class Attachment(object):
def __init__(self, part):
encoding = part.encoding or 'utf-8'
self.headers = CaseInsensitiveDict({
k.decode(encoding): v.decode(encoding)
for k, v in part.headers.items()
})
self.content_type = self.headers.get('Content-Type', None)
self.content_id = self.headers.get('Content-ID', None)
self.content_location = self.headers.get('Content-Location', None)
self._part = part
def __repr__(self):
return '<Attachment(%r, %r)>' % (self.content_id, self.content_type)
@cached_property
def content(self):
"""Return the content of the attachment
:rtype: bytes or str
"""
encoding = self.headers.get('Content-Transfer-Encoding', None)
content = self._part.content
if encoding == 'base64':
return base64.b64decode(content)
elif encoding == 'binary':
return content.strip(b'\r\n')
else:
return content
示例8: MockResponse
class MockResponse(object):
"""
Mock response object with a status code and some content
"""
def __init__(self, status_code, content=None, headers=None):
self.status_code = status_code
self.content = content or ''
self.headers = CaseInsensitiveDict()
if headers:
self.headers.update(headers)
def raise_for_status(self):
http_error_msg = ''
if 400 <= self.status_code < 500:
http_error_msg = '%s Client Error: ...' % self.status_code
elif 500 <= self.status_code < 600:
http_error_msg = '%s Server Error: ...' % self.status_code
if http_error_msg:
raise requests.HTTPError(http_error_msg, response=self)
def json(self, **kwargs):
return json.loads(self.content)
示例9: test_get
def test_get(self):
cid = CaseInsensitiveDict()
cid["spam"] = "oneval"
cid["SPAM"] = "blueval"
self.assertEqual(cid.get("spam"), "blueval")
self.assertEqual(cid.get("SPAM"), "blueval")
self.assertEqual(cid.get("sPam"), "blueval")
self.assertEqual(cid.get("notspam", "default"), "default")
示例10: test_preserve_last_key_case
def test_preserve_last_key_case(self):
cid = CaseInsensitiveDict({"Accept": "application/json", "user-Agent": "requests"})
cid.update({"ACCEPT": "application/json"})
cid["USER-AGENT"] = "requests"
keyset = frozenset(["ACCEPT", "USER-AGENT"])
assert frozenset(i[0] for i in cid.items()) == keyset
assert frozenset(cid.keys()) == keyset
assert frozenset(cid) == keyset
示例11: test_lower_items
def test_lower_items(self):
cid = CaseInsensitiveDict({
'Accept': 'application/json',
'user-Agent': 'requests',
})
keyset = frozenset(lowerkey for lowerkey, v in cid.lower_items())
lowerkeyset = frozenset(['accept', 'user-agent'])
assert keyset == lowerkeyset
示例12: test_get
def test_get(self):
cid = CaseInsensitiveDict()
cid["spam"] = "oneval"
cid["SPAM"] = "blueval"
assert cid.get("spam") == "blueval"
assert cid.get("SPAM") == "blueval"
assert cid.get("sPam") == "blueval"
assert cid.get("notspam", "default") == "default"
示例13: set_extra_headers
def set_extra_headers(self, headers):
header_dict = CaseInsensitiveDict(headers)
if 'Reply-To' in header_dict:
self.data["ReplyTo"] = header_dict.pop('Reply-To')
self.data["Headers"] = [
{"Name": key, "Value": value}
for key, value in header_dict.items()
]
示例14: test_preserve_key_case
def test_preserve_key_case(self):
cid = CaseInsensitiveDict({
'Accept': 'application/json',
'user-Agent': 'requests',
})
keyset = frozenset(['Accept', 'user-Agent'])
assert frozenset(i[0] for i in cid.items()) == keyset
assert frozenset(cid.keys()) == keyset
assert frozenset(cid) == keyset
示例15: test_copy
def test_copy(self):
cid = CaseInsensitiveDict({
'Accept': 'application/json',
'user-Agent': 'requests',
})
cid_copy = cid.copy()
assert cid == cid_copy
cid['changed'] = True
assert cid != cid_copy