【发布时间】:2017-08-02 05:55:47
【问题描述】:
我正在关注this 以重新启动处于 INSTALLED 状态的 Ambari 组件,为此我编写了一个 python 代码使用 Pycurl 解析 Ambari 服务。
但是一旦解析了 JSON,我将生成一个 JSON 文件,例如:
{
"RequestInfo": {
"command": "START",
"context": "Restart all components on HOST"
},
"Requests/resource_filters": [
{
"component_name": "NAMENODE",
"hosts": "hadoopm",
"service_name": "HDFS"
},
{
"component_name": "RESOURCEMANAGER",
"hosts": "hadoopm",
"service_name": "YARN"
}]}
这适用于:
curl -u username:password -H 'X-Requested-By-ambari' http://ambariserver:8080/api/v1/clusters/CLUSTERNAME/requests -d@service-restart.json
但同样无法正常工作,并且使用以下代码失败并出现 400 Bad request 错误:
import pycurl
c = pycurl.Curl()
c.setopt(pycurl.URL, url_post)
c.setopt(pycurl.HTTPHEADER, ["X-Requested-By:ambari"])
data = json.dumps(json.loads(open(output_temp_file,'rb').read()), indent=1, sort_keys=True)
tabs = re.sub('\n +', lambda match: '\n' + '\t' * (len(match.group().strip('\n')) / 2), data)
tabJSON=json.dumps(json.loads(open(output_temp_file,'rb').read()), indent=1, sort_keys=True)
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.USERPWD,'admin:'+admin_pass)
c.setopt(pycurl.POSTFIELDS, 'tabJSON')
c.setopt(pycurl.WRITEFUNCTION, service_buffer.write)
c.setopt(pycurl.VERBOSE, 1)
c.perform()
c.close()
并因 HTTP/1.1 400 Bad Request
而失败我这样做有什么问题吗?有人可以帮我解决这个问题吗?
【问题讨论】:
-
当您从 python 发送请求时,ambari-server.log 中有什么消息?
标签: python python-2.7 post ambari pycurl