Python 获取根据文件URL 获取文件的基本信息

#coding=utf-8
import os,sys,time,urllib2,difflib

url = "远程文件url"
#获取文件的基本信息
def getRemoteFileInfo(url, proxy=None):

    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:
        return dict(response.headers)
        #('reponseheader_dic:    ', 
        #{'content-length': '3583', 'accept-ranges': 'bytes',
        #'server': 'nginx', 'last-modified': 'Sat, 25 Jul 2015 19:07:18 GMT', 'connection': 'close',
        # 'etag': '"55b3de66-dff"', 'date': 'Tue, 28 Jul 2015 07:42:10 GMT', 'content-type': 'application/octet-stream'})

#将GMT 时间 转换成北京时间 
def GMT2BJ(modify_time):
    ltime= time.strptime(modify_time[5:25], "%d %b %Y %H:%M:%S")
    #print(ltime)
    bj_time=time.localtime(time.mktime(ltime)+8*60*60)
    dateStr="%u-%02u-%02u"%(bj_time.tm_year,bj_time.tm_mon,bj_time.tm_mday)
    timeStr="%02u:%02u:%02u"%(bj_time.tm_hour,bj_time.tm_min,bj_time.tm_sec)
    return dateStr+' '+timeStr

#判断远程文件今天是否有更新过
def isModifyedToday(modify_time):
    modify_time = time.strptime(modify_time[5:25], "%d %b %Y %H:%M:%S")
    #转化成北京时间 
    modify_time = time.localtime(time.mktime(modify_time)+8*60*60)
    current_time = time.localtime()
    print modify_time
    print current_time
    if current_time.tm_year == modify_time.tm_year and current_time.tm_mday == modify_time.tm_mday and current_time.tm_mon == modify_time.tm_mon :
        return True
    else:
        return False

if __name__ == "__main__":
    fileInfo = getRemoteFileInfo(url)
    #gmt 时间  Sat, 25 Jul 2015 19:07:18 GMT
    modify_time = fileInfo['last-modified']
    if isModifyedToday(modify_time):
        print "文件有更新..."
        #比较文件有什么不同,并取出不同的内容
        a = open('E:/python/a.txt', 'U').readlines()
        b = open('E:/python/b.txt', 'U').readlines()
        diff = difflib.ndiff(a, b)
        sys.stdout.writelines(diff)

你可能感兴趣的:(Python)