johnmahugu

python cron job aka repeater

Jun 3rd, 2015
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.39 KB | None | 0 0
  1. import threading
  2. from threading import Timer
  3.  
  4. class cron(object):
  5.     def __init__(self, interval, function, *args, **kwargs):
  6.         self._timer     = None
  7.         self.interval   = interval
  8.         self.function   = function
  9.         self.args       = args
  10.         self.kwargs     = kwargs
  11.         self.is_running = False
  12.         self.start()
  13.  
  14.     def _run(self):
  15.         self.is_running = False
  16.         self.start()
  17.         self.function(*self.args, **self.kwargs)
  18.  
  19.     def start(self):
  20.         if not self.is_running:
  21.             self._timer = Timer(self.interval, self._run)
  22.             self._timer.start()
  23.             self.is_running = True
  24.  
  25.     def stop(self):
  26.         self._timer.cancel()
  27.         self.is_running = False
  28.  
  29. from time import sleep
  30.  
  31. def hello(name):
  32.     print "Hello %s!" % name
  33.  
  34. print "starting..."
  35. rt = cron(1, hello, "World") # it auto-starts, no need of rt.start()
  36. try:
  37.     sleep(5) # your long-running job goes here...
  38.     print "serving...."
  39.     print(' ')
  40.     print(' ')
  41.     import socket
  42.     googleip=socket.gethostbyname('www.google.com')
  43.     print 'Google public IP address is:', googleip
  44.     print('********************************************')
  45.     yourdomain_hn_ip=socket.gethostbyname('yourdomain.hn.org')
  46.     print 'yourdomain.hn.ip public IP address is:', yourdomain_hn_ip
  47.     print('********************************************')
  48.     import urllib
  49.     mypublicip = urllib.urlopen('http://www.biranchi.com/ip.php').read()
  50.     print 'My public ip is :', mypublicip
  51.     print(' ')
  52.     print(' ')
  53. finally:
  54.     rt.stop() # better in a try/finally block to make sure the program ends!
  55.     print "starting..."
  56.  
  57. #----------------------------------------------------------------------------
  58. # This example requires the requests library be installed.  You can learn more
  59. # about the Requests library here: http://docs.python-requests.org/en/latest/
  60. print(' ')
  61. print(' ')
  62. import socket
  63. googleip=socket.gethostbyname('www.google.com')
  64. print 'Google public IP address is:', googleip
  65. print('********************************************')
  66. kenya_hn_ip=socket.gethostbyname('kenya.hn.org')
  67. print 'kenya.hn.ip public IP address is:', kenya_hn_ip
  68. print('********************************************')
  69. import urllib
  70. mypublicip = urllib.urlopen('http://www.biranchi.com/ip.php').read()
  71. print 'My public ip is :', mypublicip
  72. print(' ')
  73. print(' ')
  74. import mechanize
Advertisement
Add Comment
Please, Sign In to add comment