获得下载文件的大小(Python)

>>> import urlparse
>>> import httplib
>>> parsedurl = urlparse.urlparse('http://vfile.home.news.cn/music/public/vd06/200907/23/4a/MUfs0620090723163313524a2f29.mp3')
>>> httpConn = httplib.HTTPConnection(parsedurl[1])
>>> httpConn.request('GET', parsedurl[2])
>>> response = httpConn.getresponse()
>>> if response.status == 200:
...     size = response.getheader('Content-Length')
...     size = int(size) / 1024
...     print 'Size: %s KB,Content-Type: %s, Last-Modified: %s'%(size,response.getheader('Content-Type'),response.getheader('Last-Modified'))
... else:
...     print response.status,response.reason
>>> httpConn.close()

你可能感兴趣的:(python)