How to write special characters in shell command from subprocess.Popen

p =
subprocess.Popen(’’‘curl -XGET “http://xyz:80/_sql?format=txt” -H “Content-Type: application/json” -d’{ “query”: "DESC “vmanagestats*” "}’ ‘’’ , shell=True, stdout=subprocess.PIPE)
(output, err) = p.communicate()
print(output,err)

Error: (’{“error”:{“root_cause”:[{“type”:“x_content_parse_exception”,“reason”:"[1:13] [sql/query] failed to parse object"}],“type”:“x_content_parse_exception”,“reason”:"[1:13] [sql/query] failed to parse object",“caused_by”:{“type”:“json_parse_exception”,“reason”:“Unexpected character (‘v’ (code 118)): was expecting comma to separate Object entries\n at [Source: (org.elasticsearch.common.io.stream.InputStreamStreamInput); line: 1, column: 21]”}},“status”:400}’, None)

The subprocess.Popen function accepts a sequence, not a single string as its first argument.
Try splitting the command into separate arguments that you would use in a terminal.

The easiest and safest way is to avoid shell=True and pass arguments as a list of strings. shell=True is dangerous.

subprocess.Popen(["curl", "-XGET", "http://xyz:80/_sql?format=txt", "-H", "Content-Type: application/json", "-d", """{ "query": ...}"""], stdout=subprocess.PIPE)

1 Like