#/usr/bin/env python# -*- coding: utf-8 -*- import time,datetime,socket, urllib,urllib2,cookielib# because I was going to pick up Monday of the time today so write 10 here, time should be November 2014 24 last week Monday,threedayago = ( Datetime.datetime.now () - datetime.timedelta (days = 10)) #时间格式化输出, Since the time inside the cacti is calculated with timestamps, I conveniently convert to timestamp otherstyletime = threedayago.strftime ("%y-%m-%d %h:%m:%s") print otherstyletime# because the time taken above is based on the current time and date, but I cacti the time of the plot should start from 00:00:00, so the conversion format_otherstyletime = "%s 00:00:00" % otherstyletime.split () [0]print format_otherstyletimestart= Time.mktime (Time.strptime (format_otherstyletime, '%y-%m-%d %h:%m:%s ')) print startthreedayago = (Datetime.datetime.now () - datetime.timedelta (days = 4)) otherstyletime = Threedayago.strftime ("%y-%m-%d %h:%m:%s") print otherstyletimeresult = "%s 00:00:00" % otherstyletime.split () [0]print resultend=time.mktiMe (Time.strptime (result, '%y-%m-%d %h:%m:%s ')) Print endsocket.setdefaulttimeout () headers={} Cookiejar = cookielib. Cookiejar () Urlopener = urllib2.build_opener (urllib2. Httpcookieprocessor (Cookiejar)) # loginvalues = {' action ': ' Login ', ' login_username ': ' username ', ' Login_password ': ' Password ' }data = urllib.urlencode (values) request = urllib2. Request ("HTTP://IP address/cacti/index.php", data ,headers) Res = urlopener.open (Request). Read () # Get imagerequest = urllib2. Request ("HTTP://IP address/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 filefile_object = open (' 186-1week.png ', ' WB ') file_ Object.write (RES) file_object.close ()
This article is from the "Server" blog, so be sure to keep this source http://zhangfang2012.blog.51cto.com/6380212/1586389
Python automatic login cacti get host traffic graph