【发布时间】:2021-04-30 02:49:15
【问题描述】:
我正在尝试将带有标签的字典发送到 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}}
【问题讨论】:
-
Python 3 字符串已经是 Unicode。你不需要做任何特别的事情。甚至
a也是一个 Unicode 字符。问题在于您如何向该 Web API 发出请求。您是否在请求中使用了错误的content-type? -
P3 has incorporated unicode into str不,这根本不是发生的事情。 Python 3 字符串 是 Unicode,句号。没有什么可以验证或验证的。 Unicode 不是某种转义序列,它是 all 字符转换为字节的方式。如果您对 HTTP 请求有编码问题,请发布您的 HTTP 代码 -
道歉我已经编辑了 - “max_check_attempts”之前的“u”字符,它的值是 web-api 所期望的。我对 curl 做了同样的事情 - web-api 接受 python2 格式的 'u' 前缀,但如果它不存在则不接受。
-
@PanagiotisKanavos - 它似乎与我发送到 web-api 的特定字符串有关。您认为可能是实际的 Web 请求需要将其从 python3 渲染为 python2 兼容格式?
-
所以 API 期望您通过 HTTP 将字符串
{u"foo": u"bar"}发送给它?这意味着它可能仅仅依靠str({'foo': 'bar'})并发送它?这仅适用于 Python 2,因为在 Python 3 中您不会从中获得u前缀? — 那是一个糟糕的 API,期待 特定语言,甚至更特定于特定语言的过时版本,输入格式......
标签: python python-3.x check-mk