#/usr/bin/env python # -*- coding: utf-8 -*- import time,datetime,socket,urllib,urllib2,cookielib #由于我是今天去取上周一的时间所以这里写成10,时间应该是2014年11月24上周星期一, threeDayAgo = (datetime.datetime.now() - datetime.timedelta(days = 10)) #时间格式化输出,由于cacti里面的时间是以时间戳计算的我方便转换成时间戳 otherStyleTime = threeDayAgo.strftime("%Y-%m-%d %H:%M:%S") print otherStyleTime #由于上面取到的时间是以当前的时间跟日期,但是我cacti里面出图的时间应该从00:00:00开始,所以转换 format_otherStyleTime = "%s 00:00:00" % otherStyleTime.split()[0] print format_otherStyleTime start=time.mktime(time.strptime(format_otherStyleTime,‘%Y-%m-%d %H:%M:%S‘)) print start threeDayAgo = (datetime.datetime.now() - datetime.timedelta(days = 4)) otherStyleTime = threeDayAgo.strftime("%Y-%m-%d %H:%M:%S") print otherStyleTime result = "%s 00:00:00" % otherStyleTime.split()[0] print result end=time.mktime(time.strptime(result,‘%Y-%m-%d %H:%M:%S‘)) print end socket.setdefaulttimeout(10) headers={} cookiejar = cookielib.CookieJar() urlOpener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar)) # login values = {‘action‘:‘login‘, ‘login_username‘:‘用户名‘,‘login_password‘:‘密码‘ } data = urllib.urlencode(values) request = urllib2.Request("http://ip地址/cacti/index.php", data ,headers) res = urlOpener.open(request).read() #get image request = urllib2.Request("http://ip地址/cacti/graph_image.php?local_graph_id=31&rra_id=0&view_type=tree&graph_start=%s&graph_end=%s"%(int(start),int(end)), None ,headers) res = urlOpener.open(request).read() # save image to file file_object = open(‘186-1week.png‘, ‘wb‘) file_object.write(res) file_object.close()
本文出自 “服务器” 博客,请务必保留此出处http://zhangfang2012.blog.51cto.com/6380212/1586391
原文地址:http://zhangfang2012.blog.51cto.com/6380212/1586391