Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法通过asyncio使用bash命令_Bash_Python 3.x_Python Asyncio - Fatal编程技术网

无法通过asyncio使用bash命令

无法通过asyncio使用bash命令,bash,python-3.x,python-asyncio,Bash,Python 3.x,Python Asyncio,我正在尝试生成一个简单的命令,我可以在discord中调用该命令,以查看目录中有多少个文件,但由于某些原因,我的代码在运行该命令时不断出现以下错误: `Traceback (most recent call last): File "/home/pi/MusicToaster/musicbot/bot.py", line 1995, in on_message response = await handler(**handler_kwargs) File "/home/pi/M

我正在尝试生成一个简单的命令,我可以在discord中调用该命令,以查看目录中有多少个文件,但由于某些原因,我的代码在运行该命令时不断出现以下错误:

 `Traceback (most recent call last):
  File "/home/pi/MusicToaster/musicbot/bot.py", line 1995, in on_message
    response = await handler(**handler_kwargs)
  File "/home/pi/MusicToaster/musicbot/bot.py", line 1822, in cmd_audiocache
    stdout=asyncio.subprocess.PIPE)
  File "/usr/local/lib/python3.5/asyncio/subprocess.py", line 212, in create_subprocess_exec
    stderr=stderr, **kwds)
  File "/usr/local/lib/python3.5/asyncio/base_events.py", line 970, in subprocess_exec
    bufsize, **kwargs)
  File "/usr/local/lib/python3.5/asyncio/unix_events.py", line 184, in _make_subprocess_transport
    **kwargs)
  File "/usr/local/lib/python3.5/asyncio/base_subprocess.py", line 40, in __init__
    stderr=stderr, bufsize=bufsize, **kwargs)
  File "/usr/local/lib/python3.5/asyncio/unix_events.py", line 635, in _start
    universal_newlines=False, bufsize=bufsize, **kwargs)
  File "/usr/local/lib/python3.5/subprocess.py", line 950, in __init__
    restore_signals, start_new_session)
  File "/usr/local/lib/python3.5/subprocess.py", line 1540, in _execute_child
    raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'find /home/pi/MusicToaster/audio_cache -type f | wc -l'`
我检查了正在运行的命令,我无法找到它出错的原因,当我手动键入时,我没有问题:

该值增加了,因为在我拍摄该屏幕截图时,写入该文件夹的bot正在运行

以下是该命令的代码:

async def cmd_audiocache(self, channel):
    await self.safe_send_message(channel, "hang on ill check :thinking:")
    process = await asyncio.create_subprocess_exec(
        'find /home/pi/MusicToaster/audio_cache -type f | wc -l',
        stdout=asyncio.subprocess.PIPE)
    stdout, stderr = await process.communicate()
    file_count = stdout.decode().strip()
    file_count = str(file_count)
    file_count = file_count + " songs stored"
    await self.safe_send_message(channel, file_count)
    process = await asyncio.create_subprocess_exec(
        'du /home/pi/MusicToaster/audio_cache -h',
        stdout=asyncio.subprocess.PIPE)
    stdout, stderr = await process.communicate()
    file_size = stdout.decode().strip()
    file_size = str(file_size)
    file_size = "all songs total to" + file_size
    await self.safe_send_message(channel, file_size)

请原谅代码的混乱,我不会整理代码,直到我知道它能工作。

注意以下两者之间的区别:

从一个或多个字符串参数[…]创建子流程,其中第一个字符串指定要执行的程序,其余字符串指定程序的参数

以及:

使用平台的“shell”语法从cmd[…]创建子流程

例如:

# Using exec
process = await asyncio.create_subprocess_exec('ls', '-l')
# Using shell
process = await asyncio.create_subprocess_shell('ls -l') 

因此,如果我有一个完整的shell命令作为一个长字符串,如何将其发送到
asyncio.create_subprocess_exec()
,以防止shell注入?我尝试了
commandString.split()
,但与
Popen
不同,
subprocess\u exec
不接受列表…@medley56过去,解决方案是使用
create\u subprocess\u exec(*cmdList…)
@medley56过去,解决方案是使用
shlex.quote()
方法。如果需要调用
create\u subprocess\u exec()
,则对字符串调用
split()
函数。请在此处阅读更多信息: