Python 相当于将 POST 负载用单引号括起来

tim*_*erc 2 python json splunk

与 Splunk 相比,这更像是一个 Python 问题,但如果有人这样做的话将会很有帮助......特别是在这里,有一个关于在单个 POST 中向服务器发送多个指标的讨论。他们提供的示例是使用curl 并将整个有效负载用单引号(\')括起来,例如

\n\n
curl -k http://<IP address or host name or load balancer name>:8088/services/collector  \\\n-H "Authorization: Splunk 98a1e071-bc35-410b-8642-78ce7d829083"                         \n\\\n-d \'{"time": 1505501013.000,"source":"disk","host":"host_99","fields": \n{"region":"us-west-1","datacenter":"us-west- 1a","rack":"63","os":"Ubuntu16.10","arch":"x64","team":"LON","service":"6","service_version":"0","service_environment":"test","path":"/dev/sda1","fstype":"ext3","_value":999311222774,"metric_name":"total"}}\n{"time": 1505511013.000,"source":"disk","host":"host_99","fields": \n{"region":"us-west-1","datacenter":"us-west-1a","rack":"63","os":"Ubuntu16.10","arch":"x64","team":"LON","service":"6","service_version":"0","service_environment":"test","path":"/dev/sda1","fstype":"ext3","_value":1099511627776,"metric_name":"total"}}\'\n
Run Code Online (Sandbox Code Playgroud)\n\n

我的问题是如何在 python \xe2\x80\x93 中执行相同的操作,即您不能像在curl命令中那样将多个JSON对象用单引号括起来 - 这只会使整个有效负载成为字符串。是否有其他包装可以用于此目的?

\n\n

所以,这有效:

\n\n
payload = {"time": 1505501013.000,"source":"disk","host":"host_99","fields": \n{"region":"us-west-1","datacenter":"us-west- 1a","rack":"63","os":"Ubuntu16.10","arch":"x64","team":"LON","service":"6","service_version":"0","service_environment":"test","path":"/dev/sda1","fstype":"ext3","_value":999311222774,"metric_name":"total"}}\n
Run Code Online (Sandbox Code Playgroud)\n\n

但这并不:

\n\n
payload = {"time": 1505501013.000,"source":"disk","host":"host_99","fields": \n{"region":"us-west-1","datacenter":"us-west- 1a","rack":"63","os":"Ubuntu16.10","arch":"x64","team":"LON","service":"6","service_version":"0","service_environment":"test","path":"/dev/sda1","fstype":"ext3","_value":999311222774,"metric_name":"total"}}\n {"time": 1505511013.000,"source":"disk","host":"host_99","fields": \n{"region":"us-west-1","datacenter":"us-west-1a","rack":"63","os":"Ubuntu16.10","arch":"x64","team":"LON","service":"6","service_version":"0","service_environment":"test","path":"/dev/sda1","fstype":"ext3","_value":1099511627776,"metric_name":"total"}}\n
Run Code Online (Sandbox Code Playgroud)\n\n

仅供参考,那么 POST 看起来像:

\n\n
 resp = requests.post(splunkurl,json=payload,headers=headers)\n
Run Code Online (Sandbox Code Playgroud)\n

Sla*_*lam 5

好吧,“多个 json 对象”不是一个有效的 json,除非它是一个对象列表。

一般来说,python 并不关心(就像任何其他网络工具一样),json 只是数据格式,你需要一种不同的格式。因此,您需要自己构建文本有效负载,即json.dumps(payload1) + json.dumps(payload2),并通过网络客户端将其作为“原始”数据发送。

我非常怀疑主流 http 库是否提供了这样的开箱即用的用例。


不确定投反对票的原因,即requests库(这是高级网络的事实上的标准)对有效负载进行智能处理:

requests.post(url, data={'v1': 1, 'v2': 2})  # will encode it as form data
requests.post(url, json={'v1': 1, 'v2': 2})  # will encode as json
requests.post(url, data="{'v1': 1}{'v2': 2}")  # will send as-is
Run Code Online (Sandbox Code Playgroud)

Json与http本身无关,它只是一种序列化数据的方式。大多数客户最终都会使用urllib,这根本不在乎,唯一的问题是库是否提供了发送原始数据的简单方法