Python periodically returns the URL status code && whether the Web content is JSON data

Source: Internet
Author: User

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

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.