#!/usr/bin/env python
#-*-Coding:utf:8-*-
Import Requests,sys,os,time
# Determine the number of values to know whether or not to match
If Len (sys.argv) = = 3:pass
Else
print ' Usage:%s <monitoring_name> <interval_time> '% (sys.argv[0])
Sys.exit (2)
url = ' URL address '
# Overwrite stored detection value file
Def successful_state ():
current = Int (Time.time ())
Interval = current + int (sys.argv[2])
File = open ('/tmp/check_http_status_%s.txt '%sys.argv[1], ' W ')
File.write (str (current) + ' | ' +str (interval) + ' | ' + ' 0\n ')
File.close ()
# Determine if the JSON data is
Def Status_json ():
r = Requests.get (URL)
Status = R.status_code
Try
R.json ()
print ' Ok:the current return status%s, Data as JSON format. ' %status
Except
File = open ('/tmp/check_http_status_%s.txt '%sys.argv[1], ' R ')
Read_file = File.read ()
File.close ()
Read_file_three = Read_file.split (' | ') [2]
read_file_three = Int (read_file_three) + 1
If Read_file_three > 3:
Successful_state ()
print ' Ok:the current return status%s, Data as JSON format. ' %status
Else
current = Int (Time.time ())
Interval = current + int (sys.argv[2])
File = open ('/tmp/check_http_status_%s.txt '%sys.argv[1], ' W ')
File.write (str (current) + ' | ' +str (interval) + ' | ' +str (Read_file_three))
File.close ()
print ' Warning:the current return status%s, the data was not a JSON format. ' %status
Sys.exit (1)
# Get Link status code to judge
Def check_status ():
# Use exception handling to determine if the URL is accessible (no link return code), and the time-out is 4 seconds
Try
r = Requests.get (URL, timeout=4)
Except
print ' error:unable to display the This page. '
Sys.exit (2)
Status = R.status_code
# If the link is not equal to 200 first determine if the number of detection errors is more than 3 consecutive times if more than three stops returning an error state otherwise returning an error state
If status! = 200:
current = Int (Time.time ())
Interval = current + int (sys.argv[2])
File = open ('/tmp/check_http_status_%s.txt '%sys.argv[1], ' R ')
Read_file = File.read ()
File.close ()
Read_file_three = Read_file.split (' | ') [2]
read_file_three = Int (read_file_three) + 1
If Read_file_three > 3:
Successful_state ()
Status_json ()
Else
print ' Error:the current return status%s. '%status
File = open ('/tmp/check_http_status_%s.txt '%sys.argv[1], ' W ')
File.write (str (current) + ' | ' +str (interval) + ' | ' +str (Read_file_three))
File.close ()
Sys.exit (2)
Else
Status_json ()
# detects if the file exists if it exists then determines whether the current time is greater than the specified time if it is greater than the normal detection otherwise tell the time interval
Def interval ():
current = Int (Time.time ())
Interval = current + int (sys.argv[2])
File_path = os.path.exists (R '/tmp/check_http_status_%s.txt '%sys.argv[1])
if File_path = = True:
File = open ('/tmp/check_http_status_%s.txt '%sys.argv[1], ' R ')
Read_file = File.read ()
File.close ()
read_file_two_value = Int (read_file.split (' | ') [1])
If current >= read_file_two_value:
Check_status ()
Successful_state ()
Else
print ' From the next execution interval and%s seconds. '% ( Read_file_two_value-current)
Else
filename = open ('/tmp/check_http_status_%s.txt '%sys.argv[1], ' W ')
Filename.write (str (current) + ' | ' +STR (current) + ' | ' + ' 0\n ')
Filename.close ()
print ' Run for the first time, 'll in the next Run to get the data. '
Interval()
This article is from the ", Mineral water" blog, please be sure to keep this source http://guwenqiang.blog.51cto.com/5462040/1709356
Python periodically returns the URL status code && whether the Web content is JSON data