命令行中的curl命令运行良好,但从python子进程中我得到一个错误
如果我使用curl运行以下命令,则成功:命令行中的curl命令运行良好,但从python子进程中我得到一个错误,python,subprocess,Python,Subprocess,如果我使用curl运行以下命令,则成功: curl -XPOST 'localhost:9260/icrd_client_1' -d @clientmappings.json 此命令基于json文件在elasticsearch中创建索引。我得到了令人愉快的输出: {"ok":true,"acknowledged":true} 我遇到的问题是从python脚本运行此命令时 执行脚本后,我得到的不是上面令人愉快的输出,而是以下输出: ['curl', 'XPOST', 'http://local
curl -XPOST 'localhost:9260/icrd_client_1' -d @clientmappings.json
此命令基于json文件在elasticsearch中创建索引。我得到了令人愉快的输出:
{"ok":true,"acknowledged":true}
我遇到的问题是从python脚本运行此命令时
执行脚本后,我得到的不是上面令人愉快的输出,而是以下输出:
['curl', 'XPOST', 'http://localhost:9260/icrd_client_1 -d @clientmappings.json']
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
curl: (52) Empty reply from server
Traceback (most recent call last):
File "./elastic_ops.py", line 57, in <module>
output = run_curl(command, composed_url)
File "./elastic_ops.py", line 36, in run_curl
return subprocess.check_output(cmd)
File "/usr/lib/python2.7/subprocess.py", line 544, in check_output
raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command '['curl', 'XPOST', 'http://localhost:9260/icrd_client_1 -d @clientmappings.json']' returned non-zero exit status 52
下面是我的python脚本的相关代码:
commandline_args = sys.argv
command_type = commandline_args[1]
index_name = commandline_args[2]
base_elasticsearch_url = commandline_args[3]
file_to_index = sys.argv[4] if len(sys.argv) > 4 else None
def run_curl(command, url):
cmd = ['curl', command, url]
print 'sending command: '
print cmd
return subprocess.check_output(cmd)
# create Index
# curl -XPOST 'localhost:9260/icrd_client_1' -d @clientmappings.json
if (command_type == 'create'):
print 'About to run '+command_type+' for Index: '+index_name+' from filename: '+file_to_index
command = 'XPOST'
composed_url = base_elasticsearch_url + '/' + index_name +' -d ' + '@'+file_to_index
print 'URL Request Being sent is:'
print '.... '+ composed_url
output = run_curl(command, composed_url)
print 'output:'
print output
在命令行中传递参数时,需要传递参数。所以你需要通过考试
['curl', '-XPOST', 'http://localhost:9260/icrd_client_1', '-d', '@clientmappings.json']
在命令行中传递参数时,需要传递参数。所以你需要通过考试
['curl', '-XPOST', 'http://localhost:9260/icrd_client_1', '-d', '@clientmappings.json']
您需要使用--silent选项运行curl命令,以便禁用curl响应。所以您需要像这样执行命令 下面介绍如何执行python脚本
curl--silent | sudopython-您需要使用--silent选项运行curl命令,所以您的curl响应将被禁用。所以您需要像这样执行命令 下面介绍如何执行python脚本
curl--silent | sudo python-如何将url的拆分值添加到cmd数组?@TazMan您正在编写列表,不需要拆分url,只需拆分参数:)但这会将拆分的命令列表split_commands=url.split('')print'发送命令:'print cmd.append追加到cmd中(split_命令)如何将url的拆分值添加到cmd数组?@TazMan您正在编写列表,您不需要拆分url,只需拆分参数:)但这会将拆分的命令列表split_commands=url.split(“”)print'发送命令:'print cmd.append(split_命令)注意:您的
curl
命令可以在纯Python中实现,无需子进程:从urllib2导入urlopen;输出=urlopen('http://localhost:9260/icrd_client_1,open('clientmappings.json','rb').read()).read()
注意:您的curl
命令可以在纯Python中实现,无需子进程:从urllib2导入urlopen;输出=urlopen('http://localhost:9260/icrd_client_1,打开('clientmappings.json','rb').read()).read()