本文整理汇总了Python中urllib3.exceptions.ResponseError方法的典型用法代码示例。如果您正苦于以下问题:Python exceptions.ResponseError方法的具体用法?Python exceptions.ResponseError怎么用?Python exceptions.ResponseError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib3.exceptions
的用法示例。
在下文中一共展示了exceptions.ResponseError方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: retrieve
# 需要导入模块: from urllib3 import exceptions [as 别名]
# 或者: from urllib3.exceptions import ResponseError [as 别名]
def retrieve(self):
"""Retrieve credential value and its expiry from IAM EC2."""
# Get role names.
creds_path = "/latest/meta-data/iam/security-credentials"
url = self._endpoint + creds_path
res = self._http_client.urlopen("GET", url)
if res.status != 200:
raise HTTPError(
"request failed with status {0}".format(res.status),
)
role_names = res.data.decode("utf-8").split("\n")
if not role_names:
raise ResponseError("no role names found in response")
# Get credentials of first role.
url = self._endpoint + creds_path + "/" + role_names[0]
res = self._http_client.urlopen("GET", url)
if res.status != 200:
raise HTTPError(
"request failed with status {0}".format(res.status),
)
data = json.loads(res.data)
if data["Code"] != "Success":
raise ResponseError(
"credential retrieval failed with code {0}".format(
data["Code"]),
)
try:
expiration = datetime.strptime(data["Expiration"], RFC3339NANO)
except ValueError:
expiration = datetime.strptime(data["Expiration"], RFC3339)
return Value(
data["AccessKeyId"],
data["SecretAccessKey"],
session_token=data["Token"],
), expiration + timedelta(minutes=5)