2012-05-07 99 views
1

如果我想從python 2.7 調用像終端(ubuntu)那樣的命令,我應該怎麼做,所以我想使用它可以在ubuntu的終端中使用的nfc-mfclassic ...某人可以幫我使用的是Python,請..如何在Python中使用子進程模塊

我跑這件事:在終端nfc-mfclassic r a dumptest.mfd(Ubuntu的)

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 

回答

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 ('[email protected]') 
child.expect ('ftp> ') 
child.sendline ('cd pub') 
child.expect('ftp> ') 
child.sendline ('get ls-lR.gz') 
child.expect('ftp> ') 
child.sendline ('bye') 
+0

非常感謝,這對我很有用 – user1376294

相關問題