开发者

Programmatically checking whether a website is working or not?

开发者 https://www.devze.com 2023-04-06 06:07 出处:网络
I have a WordPress self hosted blog which was down until last week. After updating WordPress, now the site is working fine. But I would like to check it frequently for next couple of days. Is it possi

I have a WordPress self hosted blog which was down until last week. After updating WordPress, now the site is working fine. But I would like to check it frequently for next couple of days. Is it possible to write a program to do this so that I can schedule it?

Please give some suggestions. I am t开发者_JS百科hinking of Python as the language for the program, but I am open to any language.


You can test if the website is up or down with a simple HTTPConnection, send a "OPTIONS *" request, if the answer is "200 OK", your site is up, otherwise, check the http error codes.

import httplib
connection = httplib.HTTPConnection(your_host, port_probably_80)
connection.request("OPTIONS", "*")
response = connection.getresponse()
if response.status != httplib.OK or response.reason != "OK":
   print "Down"
else:
   print "Up :)"


Perform HTTP query and see if the result is 200 OK. You can do this easily with PycURL

Sample from the documentation:

import pycurl
c = pycurl.Curl()
c.setopt(pycurl.URL, "http://example.com")
c.setopt(pycurl.FOLLOWLOCATION, 1)
c.perform()
print c.getinfo(pycurl.HTTP_CODE)


Well recently I went through the following blog. Gives some good ideas to approach the problem.

http://blogs.msdn.com/b/vijay/archive/2009/04/23/how-to-check-programmatically-if-a-website-is-running.aspx


Well first thing i can think about is simple a solution if you want to check this from local computer and you have xampp or something similar installed. you just need to write php script which will connects to your website through php fopen and read something. If you will get content, it means your website is up. just write something like:

// Website url to open
$websiteUrl = 'http://website.com/is_up;
$handle = fopen($websiteUrl, "r");


Your program should send a get request to the website, receive the html (verify you get "200 OK"), and compare the beginning of the string to what you know it should be (compare everything until the first thing that depends on content). If the comparison fails, then you should suspect that your site may be down, and check it yourself.


This is best suited for a little script, don't even need python for it:

while curl -q http://yoursite | grep -q some_string; do sleep 120; done; date; echo Site offline

Where some_string would be some string that’s only present, when your site is working fine (i.e. some content from the database which would not be present if a) php is not working or b) the database connection died or c) the site is completely offline.


The bash script below might be helpful to whomever ends their search here.

Note the fakePhrase is used to detect ISP "Search Assist" adware HTTP responses.

#!/bin/bash

fakePhrase="verizon"
siteList=(
  'http://google.com'
  'https://google.com'
  'http://wikipedia.org'
  'https://wikipedia.org'
  'http://cantgettherefromhere'
  'http://searchassist.verizon.com'
)

exitStatus=0

function isUp {
  http=`curl -sL -w "%{http_code}" "$1" -o temp_isUp`
  fakeResponse=`cat temp_isUp | grep $fakePhrase`
  if [ -n "$fakeResponse" ]; then
    http=$fakePhrase
  fi
  case $http in
  [2]*)
    ;;
  [3]*)
    echo 'Redirect'
    ;;
  [4]*)
    exitStatus=4
    echo "$1 is DENIED with ${http}"
    ;;
  [5]*)
    exitStatus=5
    echo "$1 is ERROR with ${http}"
    ;;
  *)
    exitStatus=6
    echo "$1 is NO RESPONSE with ${http}"
    ;;
  esac
}

for var in "${siteList[@]}"
do
  isUp $var
done

if [ "$exitStatus" -eq "0" ]; then
  echo 'All up'
fi

rm temp_isUp
exit $exitStatus
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号