>>> 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)
最新推荐文章于 2024-04-03 17:17:41 发布
本文通过Python代码演示了如何使用urlparse和httplib模块解析并获取HTTP资源的大小、类型及最后修改时间等信息。具体步骤包括解析URL、建立连接、发送GET请求并解析返回的响应头部。
149

被折叠的 条评论
为什么被折叠?



