threads in Python simple

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import threading
import time
import requests

"""
Checking http reposne every milisecond.
"""

class HttpChecking(object):

def __init__(self, interval=0.01):

self.interval = interval

thread = threading.Thread(target=self.run, args=())
thread.daemon = True
thread.start()

def check_url(self):
try:
self.url = "http://www.linuxhow.tk"
r = requests.head(self.url)
print r.status_code
except requests.ConnectionError:
print "failed to connect"

def run(self):
while True:
print('Checking HTTP status...')
self.check_url()
time.sleep(self.interval)

chk = HttpChecking()
chk.run()

Leave a Reply

Your email address will not be published. Required fields are marked *