如果我想从python 2.7调用终端(ubuntu)这样的命令,我该怎么办? 所以我想使用它能够在ubuntu终端中使用的nfc-mfclassic ...有人可以帮我在python中使用它...
我在终端(Ubuntu)中运行这个东西:nfc-mfclassic r a dumptest.mfd
Usage: nfc-mfclassic r|w a|b <dump.mfd> [<keys.mfd>]
r|w - Perform read from (r) or write to (w) card
a|b - Use A or B keys for action
<dump.mfd> - MiFare Dump (MFD) used to write (card to MFD) or (MFD to card)
<keys.mfd> - MiFare Dump (MFD) that contain the keys (optional)
Or: nfc-mfclassic x <dump.mfd> <payload.bin>
x - Extract payload (data blocks) from MFD
<dump.mfd> - MiFare Dump (MFD) that contains wanted payload
<payload.bin> - Binary file where payload will be extracted
答案 0 :(得分:1)
您可以直接使用子进程,但是有一些非常好的子进程包装器可以让您的生活更轻松。
我喜欢PBS:
PBS是一个独特的子进程包装器,可以动态地将系统程序映射到Python函数。 PBS通过Python的所有功能和灵活性为您提供Bash(简单的命令调用,简单管道)的良好功能,帮助您编写Python脚本脚本。
示例:
import pbs
print pbs.nfc_mfclassic("r", "a", "dumptest.mfd")
如果您想处理迭代应用程序,也许您应该寻找类似pyexpect的内容:
# This connects to the openbsd ftp site and
# downloads the recursive directory listing.
import pexpect
child = pexpect.spawn ('ftp ftp.openbsd.org')
child.expect ('Name .*: ')
child.sendline ('anonymous')
child.expect ('Password:')
child.sendline ('noah@example.com')
child.expect ('ftp> ')
child.sendline ('cd pub')
child.expect('ftp> ')
child.sendline ('get ls-lR.gz')
child.expect('ftp> ')
child.sendline ('bye')
答案 1 :(得分:0)
>>> import subprocess
>>> command = raw_input()
nfc-mfclassic r a dumptest.mfd
p = subprocess.Popen(command)
命令正是您在shell cmdline中键入的内容。困难的部分是正确格式化命令文本。
参考:http://docs.python.org/library/subprocess.html#module-subprocess