2017-07-14 100 views
0

马上蝙蝠,有类似的问题here但它不完全回答我的问题。尽管如此,我留下了评论。我可以将一个长bash命令传递给asyncio.subprocess.create_subprocess_exec()吗?

asyncio中有一个asyncio.subprocess.create_subprocess_exec协议模仿更常见的subprocess.Popen命令与shell=False来防止shell注入。 Popen接受字符串列表create_subprocess_exec只接受字符串,如

asyncio.subprocess.create_subprocess_exec('ls', '-lah', 'myfile', stdout=..., stderr=..., stdin=...) 

我有一个bash命令作为一个大的字符串,例如s = 'ls -lah myfile'。我想将命令传递给create_subprocess_exec,但像s.split()这样的列表不起作用!怎么办?

回答

0

似乎asyncio.subprocess.create_subprocess_exec将接受星号的列表作为一系列的参数,因此该解决方案应该是

cmd = ['ls', '-lah', 'myfile'] 
asyncio.subprocess.create_subprocess_exec(*cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, stdin=asyncio.subprocess.PIPE) 

提醒一句,用这种方式格式化的bash命令是很困难的,尤其是与rsync我发现。

相关问题