加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

通过python3子进程发送管道命令

发布时间:2020-12-20 11:47:40 所属栏目:Python 来源:网络整理
导读:我试图通过 python3.4执行以下子进程命令 cd /home/mailer-domains/domain | rndc loadkeys domain 我尝试了很多使用.call和.Popen的方法,但它不喜欢我的管道,或者它不喜欢我的开关 subprocess.call(['cd /home/mailer-domains/'+domain,'|','rndc','loadkey
我试图通过 python3.4执行以下子进程命令

cd /home/mailer-domains/domain | rndc loadkeys domain

我尝试了很多使用.call和.Popen的方法,但它不喜欢我的管道,或者它不喜欢我的开关

>>> subprocess.call(['cd /home/mailer-domains/'+domain,'|','rndc','loadkeys',domain])    
Traceback (most recent call last):
  File "<stdin>",line 1,in <module>
  File "/usr/local/lib/python3.4/subprocess.py",line 537,in call
    with Popen(*popenargs,**kwargs) as p:
  File "/usr/local/lib/python3.4/subprocess.py",line 859,in __init__
    restore_signals,start_new_session)
  File "/usr/local/lib/python3.4/subprocess.py",line 1457,in _execute_child
    raise child_exception_type(errno_num,err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'cd /home/mailer-domains/lecomm.com'

>>> subprocess.call(['cd /home/ex-mailer-domains/'+domain,'&&',domain]) 
Traceback (most recent call last):
  File "<stdin>",err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'cd /home/mailer-domains/lecomm.com'


>>> subprocess.call(['cd','/home/mailer-domains/'+domain,domain])
cd: too many arguments
2


>>> subprocess.Popen(['cd',domain])    
<subprocess.Popen object at 0x805aa5860>
cd: too many arguments


>>> subprocess.call(['cd','/home/mailer-domains/'+domain] '&&' ['rndc',domain])    
  File "<stdin>",line 1
    subprocess.call(['cd',domain])
                                                                 ^
SyntaxError: invalid syntax


>>> subprocess.call(['cd','/home/mailer-domains/'+domain],'&&' ['rndc',domain])
Traceback (most recent call last):
  File "<stdin>",in <module>
TypeError: string indices must be integers


>>> subprocess.call(['cd',['rndc',domain])     
Traceback (most recent call last):
  File "<stdin>",line 767,in __init__
    raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer


>>> subprocess.call(['cd','/home/mailer-domains/'+domain] ['rndc',in <module>
TypeError: list indices must be integers,not tuple


>>> subprocess.call(['cd','/home/mailer-domains/'+domain] +['rndc',domain])
cd: too many arguments
2


>>> ps = subprocess.Popen((['cd','/home/mailer-domains/'+domain]),stdout = subprocess.PIPE)        
>>> output = subprocess.check_output((['rndc',domain]),stdin=ps.stdout)
rndc: 'loadkeys' failed: not found
no matching zone 'lecomm.com' in any view
Traceback (most recent call last):
  File "<stdin>",line 620,in check_output
    raise CalledProcessError(retcode,process.args,output=output)
subprocess.CalledProcessError: Command '['rndc','lecomm.com']' returned non-zero exit status 1


>>> output = subprocess.check_output((['rndc',stdin=ps.stdout,stdout=PIPE)
Traceback (most recent call last):
  File "<stdin>",in <module>
NameError: name 'PIPE' is not defined

如何通过Python3子进程正确发送2个管道命令?

解决方法

答案可以在 subprocess documentation中找到.

子进程模块中的函数通常不会调用shell来解释命令,而是直接使用给定的参数调用它们!使用参数shell = True(Python文档中的示例)可以覆盖此行为:

output = check_output("dmesg | grep hda",shell=True)

但是,如果命令和参数未修复但取决于用户输入,则不建议这样做.然后,正确的方法是使用两个Popen调用并手动构造管道(代码示例再次来自Python文档):

p1 = Popen(["dmesg"],stdout=PIPE)
p2 = Popen(["grep","hda"],stdin=p1.stdout,stdout=PIPE)
p1.stdout.close()  # Allow p1 to receive a SIGPIPE if p2 exits.
output = p2.communicate()[0]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读