Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A python script that checks multiple websites status.
#!/usr/bin/env python
import os, time, httplib
from termcolor import colored
SITES = [
"www.meesters-id.nl",
"www.carolsingers.nl",
]
while 1:
for site in SITES:
conn = httplib.HTTPConnection(site, timeout=10)
conn.request("HEAD", "/")
response = conn.getresponse()
if response.status != 200:
print "\a"
response.status = colored(response.status, 'red')
print "{0:30} {1:10} {2:10}".format(site, response.status, response.reason)
conn.close()
time.sleep(2)
os.system("clear")
@MartinThoma

This comment has been minimized.

Copy link

MartinThoma commented May 30, 2019

Python 3:

#!/usr/bin/env python
import requests
from collections import namedtuple

WebsiteStatus = namedtuple('WebsiteStatus', ['status_code', 'reason'])
names = ['foo', 'bar']


def get_status(site):
    try:
        response = requests.head(site, timeout=5)
        status_code = response.status_code
        reason = response.reason
    except requests.exceptions.ConnectionError:
        status_code = '000'
        reason = 'ConnectionError'
    website_status = WebsiteStatus(status_code, reason)
    return website_status


for name in names:
    site = 'http://{}.com'.format(name)
    website_status = get_status(site)
    print("{0:30} {1:10} {2:10}"
          .format(site, website_status.status_code, website_status.reason))
@mtleiva

This comment has been minimized.

Copy link

mtleiva commented Jul 19, 2019

Python 3:

#!/usr/bin/env python
import requests
from collections import namedtuple

WebsiteStatus = namedtuple('WebsiteStatus', ['status_code', 'reason'])
names = ['foo', 'bar']


def get_status(site):
    try:
        response = requests.head(site, timeout=5)
        status_code = response.status_code
        reason = response.reason
    except requests.exceptions.ConnectionError:
        status_code = '000'
        reason = 'ConnectionError'
    website_status = WebsiteStatus(status_code, reason)
    return website_status


for name in names:
    site = 'http://{}.com'.format(name)
    website_status = get_status(site)
    print("{0:30} {1:10} {2:10}"
          .format(site, availability.status_code, availability.reason))

Hi, I had to edit the last line of code and replace it with:
.format(site, website_status.status_code, website_status.reason))

@MartinThoma

This comment has been minimized.

Copy link

MartinThoma commented Jul 24, 2019

Thank you! I fixed it!

@CoveTech

This comment has been minimized.

Copy link

CoveTech commented Feb 24, 2020

#!/usr/bin/env python
import requests
from collections import namedtuple

WebsiteStatus = namedtuple('WebsiteStatus', ['status_code', 'reason'])
names = ['foo', 'bar']


def get_status(site):
    try:
        response = requests.head(site, timeout=5)
        status_code = response.status_code
        reason = response.reason
    except requests.exceptions.ConnectionError:
        status_code = '000'
        reason = 'ConnectionError'
    website_status = WebsiteStatus(status_code, reason)
    return website_status


for name in names:
    site = 'http://{}.com'.format(name)
    website_status = get_status(site)
    print("{0:30} {1:10} {2:10}"
          .format(site, website_status.status_code, website_status.reason))

Had to change out "Availability. back to website_status"

@radoslawoska

This comment has been minimized.

Copy link

radoslawoska commented Apr 18, 2020

for some sites you might get 403 so you can do
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}

response = requests.head(site, timeout=5, allow_redirects=True, headers=headers)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.