我是Python的新手,目前正在参加道德黑客课程。他们讨论了子流程模块,以及如何使用列表来运行命令比使用字符串更安全。
例如(更改接口的MAC地址的代码):
subprocess.call(['ifconfig',interface,'down'])
subprocess.call(['ifconfig','hw','ether',new_mac])
subprocess.call(['ifconfig','up'])
这被认为比这更安全:
subprocess.call('ifconfig ' + interface + ' down',shell=True)
subprocess.call('ifconfig ' + interface + ' hw ' + ' ether ' + new_mac,shell= True)
subprocess.call('ifconfig ' + interface + ' up',shell=True)
我不理解的是Python如何理解这两个代码以及为什么您不能用列表劫持这两个代码,但可以用字符串劫持一个代码之间的区别。