實際上,我試圖將下面的代碼傳遞給bash。但我認爲tshark和pythonic%s的%s是以某種方式混合的。它沒有提供足夠的參數格式字符串錯誤。將參數傳遞給bash時子進程會混淆
def traffic_dest_port(self,filepcap):
cmd = "tshark -o column.format:'\"Source\", \"%s\", \"Destination\", \"%d\", \"dstport\", \"%uD\"' -r %s |sort|uniq " %filepcap
subprocess.call(cmd,shell=True)
你能幫我解決嗎?
謝謝。
兩件事情,這將使你的代碼更容易寫,讀,並調試:(1)使用三引號字符串而不是轉義引號,(2)讓Python使用['shlex.quote']爲你做一些工作(http://docs.python.org/ 3.3 /庫/ shlex.html#shlex.quote)。 – abarnert
有一件事會讓它更容易:如果你不需要的話,不要使用shell。請參閱子流程文檔中的[替換外殼管道](http://docs.python.org/3.3/library/subprocess.html#replacing-shell-pipeline)。 – abarnert