将终端命令转换为python脚本

时间:2021-10-20 20:23:48

How does one convert this terminal command to python? Here's the code I have tried, and I need some help with it. The values I want to pass in are < | grep "http" > including the file itself, but I get an error for passing in more than one value.

如何将此终端命令转换为python?这是我尝试过的代码,我需要一些帮助。我要传入的值是<| grep“http”>包括文件本身,但是传入多个值时出错。

Terminal command: swfdump -a /home/cfc/swf/swf/flash2.swf | grep 'http'

终端命令:swfdump -a /home/cfc/swf/swf/flash2.swf | grep'http'

Try 1: dump = Popen(["swfdump", "-a", filename, "|", "grep", "'http'"])

尝试1:dump = Popen([“swfdump”,“ - a”,filename,“|”,“grep”,“'http'”])

Try 2: dump = check_output(["swfdump", "-a", filename , "| grep 'http'"])

尝试2:dump = check_output([“swfdump”,“ - a”,filename,“| grep'htt'”])

ERROR:

Try 1:

Only one file allowed. You supplied at least two. (/home/cfc/swf/swf/flash2.swf and |)
Only one file allowed. You supplied at least two. (| and grep)
Only one file allowed. You supplied at least two. (grep and 'http')
Couldn't open 'http': No such file or directory
Only one file allowed. You supplied at least two. (/home/cfc/swf/swf/flash2.swf and |)
Only one file allowed. You supplied at least two. (| and grep)
Only one file allowed. You supplied at least two. (grep and 'http')
Couldn't open 'http': No such file or directory

Try 2:

Only one file allowed. You supplied at least two. (/home/cfc/swf/swf/flash2.swf and | grep 'http')
Couldn't open | grep 'http': No such file or directory
Traceback (most recent call last):
File "./SWFFile.py", line 62, in <module>
main()
File "./SWFFile.py", line 61, in main
trid()
File "./SWFFile.py", line 34, in trid
swfDump()
File "./SWFFile.py", line 48, in swfDump
dump = check_output(["swfdump", "-a", filename , "| grep 'http'"] )
File "/usr/lib/python2.7/subprocess.py", line 573, in check_output
raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command '['swfdump', '-a', '/home/cfc/swf/swf/flash2.swf', "| grep 'http'"]' returned non-zero exit status 1

2 个解决方案

#1


1  

The pipe command "|" is a shell construct and is meaningless to python. When you execute Popen with a list and without shell=True, the shell is bypassed and the pipe symbol is just passed to the executing program as a command line argument. Confused programs spit out errors in return.

管道命令“|”是一个shell构造,对python没有意义。当您使用列表执行Popen且没有shell = True时,将绕过shell并将管道符号作为命令行参数传递给正在执行的程序。困惑的程序会回报错误。

You could run through the shell either by converting the command to a string or by letting python convert the command to a string for you:

您可以通过将命令转换为字符串或让python将命令转换为字符串来运行shell:

dump = Popen("swfdump -a {} | grep 'http'".format(filename),
     shell=True)

dump = Popen(subprocess.list2cmdline(
    ["swfdump", "-a", filename, "|", "grep", "'http'"]),
    shell=True)

Or, do the searching in python itself

或者,在python本身进行搜索

proc = Popen(["swfdump", "-a", filename], stdout=subprocess.PIPE)
for line in proc.stdout:
    if 'http' in line:
        print(line.strip())
proc.wait()

#2


1  

If you are interested in running a lot of external commands from Python, the result is simpler and more readable using the sh module:

如果您对从Python运行大量外部命令感兴趣,那么使用sh模块的结果更简单,更易读:

from sh import swfdump, grep

grep(swfdump('-a', '/home/cfc/swf/swf/flash2.swf'), 'http')

Note that grepping may be better done using Python than an external command:

请注意,使用Python比使用外部命令更好地完成grepping:

from sh import swfdump

for line in swfdump('-a', '/home/cfc/swf/swf/flash2.swf').split('\n'):
    if 'http' in line:
        print(line)

#1


1  

The pipe command "|" is a shell construct and is meaningless to python. When you execute Popen with a list and without shell=True, the shell is bypassed and the pipe symbol is just passed to the executing program as a command line argument. Confused programs spit out errors in return.

管道命令“|”是一个shell构造,对python没有意义。当您使用列表执行Popen且没有shell = True时,将绕过shell并将管道符号作为命令行参数传递给正在执行的程序。困惑的程序会回报错误。

You could run through the shell either by converting the command to a string or by letting python convert the command to a string for you:

您可以通过将命令转换为字符串或让python将命令转换为字符串来运行shell:

dump = Popen("swfdump -a {} | grep 'http'".format(filename),
     shell=True)

dump = Popen(subprocess.list2cmdline(
    ["swfdump", "-a", filename, "|", "grep", "'http'"]),
    shell=True)

Or, do the searching in python itself

或者,在python本身进行搜索

proc = Popen(["swfdump", "-a", filename], stdout=subprocess.PIPE)
for line in proc.stdout:
    if 'http' in line:
        print(line.strip())
proc.wait()

#2


1  

If you are interested in running a lot of external commands from Python, the result is simpler and more readable using the sh module:

如果您对从Python运行大量外部命令感兴趣,那么使用sh模块的结果更简单,更易读:

from sh import swfdump, grep

grep(swfdump('-a', '/home/cfc/swf/swf/flash2.swf'), 'http')

Note that grepping may be better done using Python than an external command:

请注意,使用Python比使用外部命令更好地完成grepping:

from sh import swfdump

for line in swfdump('-a', '/home/cfc/swf/swf/flash2.swf').split('\n'):
    if 'http' in line:
        print(line)