#!/usr/bin/python import os, sys, re, subprocess #argument checking if len(sys.argv) <= 1: print "Usage:%s URL [md5]" % (sys.argv[0]) sys.exit(0) ] print "Downloading..." args = ["/usr/bin/wget", sys.argv[1] p = subprocess.Popen(args) p.wait() print "Checking MD5..." filename = re.split("/", sys.argv[1])[-1] if len(sys.argv) == 3: md5file = open("tmp.md5", "w+") md5file.write("%s %s\n" % (sys.argv[2], filename)) md5file.close() p = subprocess.Popen(["/usr/bin/md5sum", "-c", "tmp.md5"]) p.wait() os.remove("tmp.md5")整個script做的動作是
1.下載檔案,並解析該檔名
2.如果有提供MD5就進行MD5 check
另外subprocess是2.4版後用來執行系統中程式用的
class subprocess.Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)
可以只給arg,用sequence的形式,好處是都可以直接自行指定stdin, stdout, stderr。