Python automatic login cacti get host traffic graph

Source: Internet
Author: User
Tags time and date timedelta

#/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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.