python - 在 python3 dict 中包含 unicode 字符
问题描述
我正在尝试将带有标签的字典发送到 check_mk web-api - 但是 web-api 使用 python2 - 并且开发人员坚持要验证标签是 Unicode。
我在 p3 中的示例字典:
"": [
{
"condition": {
"service_description": [
{
"$regex": "Callcentre Queue: OTU"
}
]
},
"value": {
u"max_check_attempts": u"3"
},
"options": {
"description": "Label - max_check_attempts\nService - Callcentre Queue: OTU"
}
}]
但是当发送时,我得到了label max_check_attempts must be unicode type
错误。
Traceback (most recent call last):
File "./cmk_import.py", line 415, in <module>
process_service_rulelist()
File "./cmk_import.py", line 309, in process_service_rulelist
api().set_ruleset('service_label_rules', total_lbl_ruleset)
File "/usr/local/lib/python3.6/dist-packages/check_mk_web_api/__init__.py", line 724, in set_ruleset
return self.make_request('set_ruleset', data=data, query_params={'request_format': 'python'})
File "/usr/local/lib/python3.6/dist-packages/check_mk_web_api/__init__.py", line 164, in make_request
raise CheckMkWebApiException(result)
check_mk_web_api.exception.CheckMkWebApiException: Check_MK exception: ERROR: The label ID 'max_check_attempts' is of type <type 'str'>, but should be unicode. Affected Rule {'value': {'max_check_attempts': '3'}, 'condition': {'service_description': [{'$regex': 'Callcentre Queue: OTU'}]}, 'options': {'description': 'Label - max_check_attempts\nService - Callcentre Queue: OTU'}}
错误来自 check_mk web-api 发出的 checkMK 本身。由于监听代理是 Python2 - 它试图验证type()
为 unicode
我知道 P3 已合并unicode
到str
- 但想知道是否有一种方法可以覆盖和保留 dict unicode 字符而不用重写受影响的部分python2
。
Web 请求由这个库处理 - https://github.com/brennerm/check-mk-web-api
request = urllib.request(self._build_request_path(query_params),WebApi._build_request_data(data, request_format))
其中内容为: Arg1:
http://localhost/preprod/check_mk/webapi.py?effective_attributes=0&action=get_all_hosts&_username=automation&_secret=foobar
和片段:
{%27condition%27: {%27service_description%27: [
{%27%24regex%27: %27VPN: Premier Ping - 1.1.1.1%27}
]}, %27value%27:
{%27check_interval%27: %275%27, %27max_check_attempts%27: %273%27, %27retry_interval%27: %271%27, %27check_period%27: %2724x7%27, %27notification_period%27: %2724x7%27, %27notification_interval%27: %27120%27, %27notifications_enabled%27: %271%27, %27active_checks_enabled%27: %271%27, %27check_freshness%27: %270%27, %27event_handler_enabled%27: %271%27, %27flap_detection_enabled%27: %271%27, %27is_volatile%27: %270%27, %27obsess_over_service%27: %271%27, %27passive_checks_enabled%27: %271%27, %27process_perf_data%27: %271%27, %27retain_nonstatus_information%27: %271%27, %27retain_status_information%27: %271%27}
, %27options%27: {%27description%27: %27Labels for Service - VPN: Premier Ping - 1.1.1.1%27}}
解决方案
API 客户端以它所谓的“python”格式发送数据,这基本上是一个已经str()
调用它的字典。目的是服务器将调用ast.literal_eval()
此字典字符串。客户端对响应的正文执行相同的操作。将其转换回 Python 字典。
在整个 API 中,有些地方确实支持 JSON 进行请求/响应处理,这可以在某些函数的查询参数中设置。不幸的是,对于此处失败的特定请求 (set_ruleset),JSON 不是一个选项。
服务器无法处理此请求的原因尚不清楚。我尝试取消引用消息的正文并literal_eval()
在 Python 2 中运行它,它似乎有效。
我确实注意到的一件事是 request dict 中嵌入的换行符"Label - max_check_attempts\nService - Callcentre Queue: OTU"
。这可能是服务器在提到“标签 ID 'max_check_attempts'”时所抱怨的。
进一步的问题跟踪需要查看服务器日志,可能还需要查看代码以了解那里发生了什么。
推荐阅读
- redis - Redis:创建服务器 TCP 侦听套接字 *:6388:绑定:权限被拒绝
- azure - ARM 部署:获取 Azure Function API 密钥
- docker - 关于网络接口的 Ansible 未定义事实
- java - 计算递归方法的时间复杂度
- c++ - 选择选举获胜者的程序
- r - 如何使用 ggplot2 将标准错误添加到 nls 图?
- ansible - 如何在 Ansible 中检查某个状态码 (4xx)?
- python - Python - 替换列表中的特定数字
- javascript - 无法将json数据获取到jquery数据表
- javascript - 如何在 element 的 slideUp() 之前阻止滚动和 body 的高度,并在新元素的 slideDown() 之后删除 css?- jQuery