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

Friday, January 29, 2010

>>> 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()

This entry was tagged Python

comments powered by Disqus

© 2009-2013 lxneng.com. All rights reserved. Powered by Pyramid

go to Top