Python獲取遠程文件大小的函數(shù)代碼分享
更新時間:2014年05月13日 10:24:17 作者:
這篇文章主要介紹了Python獲取遠程文件大小的函數(shù)代碼分享,需要的朋友可以參考下
復(fù)制代碼 代碼如下:
def getRemoteFileSize(url, proxy=None):
""" 通過content-length頭獲取遠程文件大小
url - 目標(biāo)文件URL
proxy - 代理 """
opener = urllib2.build_opener()
if proxy:
if url.lower().startswith('https://'):
opener.add_handler(urllib2.ProxyHandler({'https' : proxy}))
else:
opener.add_handler(urllib2.ProxyHandler({'http' : proxy}))
try:
request = urllib2.Request(url)
request.get_method = lambda: 'HEAD'
response = opener.open(request)
response.read()
except Exception, e: # 遠程文件不存在
return 0
else:
fileSize = dict(response.headers).get('content-length', 0)
return int(fileSize)
您可能感興趣的文章:
相關(guān)文章
Python學(xué)習(xí)pygal繪制線圖代碼分享
這篇文章主要介紹了Python學(xué)習(xí)pygal繪制線圖代碼分享,具有一定借鑒價值,需要的朋友可以參考下。2017-12-12
淺析python中numpy包中的argsort函數(shù)的使用
這篇文章主要介紹了python中numpy包中的argsort函數(shù)的使用,argsort()函數(shù)在模塊numpy.core.fromnumeric中,本文通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧2018-08-08
python matplotlib坐標(biāo)軸設(shè)置的方法
本篇文章主要介紹了python matplotlib坐標(biāo)軸設(shè)置的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12

