Welcome, guest | Sign In | My Account | Store | Cart
###################################################################################################################

    import os
    import sys


    class Daemonize:
        """ Generic class for creating a daemon"""

        def daemonize(self):
            try:
                #this process would create a parent and a child
                pid = os.fork()
                if pid > 0:
                    # take care of the first parent
                    sys.exit(0)
            except OSError, err:
                sys.stderr.write("Fork 1 has failed --> %d--[%s]\n" % (err.errno,
                                                                  err.strerror))
                sys.exit(1)

            #change to root
            os.chdir('/')
            #detach from terminal
            os.setsid()
            # file to be created ?
            os.umask(0)

            try:
                # this process creates a parent and a child
                pid = os.fork()
                if pid > 0:
                    print "Daemon process pid %d" % pid
                    #bam
                    sys.exit(0)
            except OSError, err:
                sys.stderr.write("Fork 2 has failed --> %d--[%s]\n" % (err.errno,
                                                                  err.strerror))
                sys.exit(1)

            sys.stdout.flush()
            sys.stderr.flush()

        def start_daemon(self):
            self.daemonize()
            self.run_daemon()

        def run_daemon(self):
            pass

###################################################################################################################

    import os
    import smtplib
    from smtplib import SMTPException
    import time

    from Daemonize import Daemonize


     class WatchFile(Daemonize):

        def __init__(self, file_path, size_limit=15728640):
            self.file = os.path.realpath(file_path)
            print '---'
            assert os.path.isfile(self.file), '%s does not exist' % self.file
            print '+++'
            self.smtpserver = '*path-to-your-host*'
            self.recipient_list = ['recipient@domain']
            self.sender = 'sender@domain'
            self.file_size_limit = size_limit
            self.email_body = 'path-to-your-email-template'

        def send_an_email(self):

            email_body = open(self.email_body, 'r').read()
            session_obj = smtplib.SMTP(self.smtpserver)
            try:
                session_obj.sendmail(self.sender, self.recipient_list,
                                               email_body)
            except SMTPException:
                print "Unable to send emails."
            finally:
                session_obj.close()

        def watch(self):
            current_file_size = os.path.getsize(self.file)
            if current_file_size > self.file_size_limit:
                self.send_an_email()

        def run_daemon(self):
            while 1:
                self.watch()
                time.sleep(3600)


    if __name__ == '__main__':
        watchdog = WatchFile('path-to-your-inbox')
        watchdog.start_daemon()

History