Python urllib從遠程服務器下載文件到本地:
- #!/usr/bin/env python
- #-*-coding:utf-8-*-'
- #Filename:download_file.py
- import sys,os
- import urllib
- def urlcallback(a,b,c):
- """
- call back function
- a,已下載的數據塊
- b,數據塊的大小
- c,遠程文件的大小
- """
- print "callback"
- prec=100.0*a*b/c
- if 100 < prec:
- prec=100
- print "%.2f%%"%(prec,)
-
- def main(argv):
- """
- main
- """
- print "start..."
- urllib.urlretrieve("http://jaist.dl.sourceforge.net/project/pydev/pydev/Pydev%201.6.3/org.python.pydev.feature-1.6.3.2010100513.zip"/
- ,"/tmp/python.pydev.zip"/
- ,urlcallback)
- print "end..."
-
- if __name__=="__main__":
- main(sys.argv[1:])