0

I'm trying to send a command through a socket from client to server to execute a command on the server and send the output back to me. Everything works fine if the command is one word with no options. However if I use an option such as netstat -an or dir c:\ then the command isn't recognized and from the output it looks like quotes are put around the command before being executed ('"netstat -an"' is not recognized as an internal or external command). I know they aren't saved in the variable this way because I printed it before being executed to error check. Please help. Here is what my code looks like:

commout = subprocess.Popen([data], stdout=subprocess.PIPE, shell=True)

(out, err) = commout.communicate()
4

2 回答 2

0

尝试制作data一个参数数组(第一个是实际命令)。

例如:

commout = subprocess.Popen(['netstat', '-an'], stdout=subprocess.PIPE, shell=True)

第一个元素是表示实际命令的字符串 ( netstat),下一个元素是表示第一个参数 ( -an) 的字符串。

澄清一下,Popen(['echo', 'a', 'b']相当于echo a b在命令行上,而Popen(['echo', 'a b']将相当于代替(即,在andecho "a b"之间有空格的单个参数。ab

于 2013-04-19T23:07:14.973 回答
0

如果您传递一个列表,则其中的每个项目将单独引用。在这种情况下,只需传递一个字符串:

subprocess.Popen(data, stdout=subprocess.PIPE, shell=True)
于 2013-04-19T23:08:04.997 回答