Quantcast

Wanted to know how we can overwrite the win32serviceutil.ServiceFramework SvcDoRun method.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Wanted to know how we can overwrite the win32serviceutil.ServiceFramework SvcDoRun method.

pavi ena
Hello,

I am new to Python Window services development. I have small code below.

from datetime import datetime
import time
import sys
import os

import win32service
import win32serviceutil
import win32api
import win32con

class DemoService(win32serviceutil.ServiceFramework):
    _svc_name_ = "DemoService"
    _svc_display_name_ = "DemoService"
   
    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.isAlive = True
        self.args =  args
   
    def SvcDoRun(self):
        import servicemanager
        while self.isAlive:
            start_mail_check(args[2], args[3])
            self.isAlive = False

    def SvcStop(self):
        import servicemanager       
        servicemanager.LogInfoMsg("ZetlConductorSvcs - Received stop signal")
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        self.isAlive = False
   
def ctrlHandler(ctrlType):
    return True
   
def start_mail_check(user_id=None, passwd=None):
    if not user_id and not passwd:
        servicemanager.LogInfoMsg("DemoService - Invalid argument passed.")
    else:
       #Go ahead to download the mails from configured mail server.
  
def main():
   print ">>>", sys.argv, len(sys.argv)
   
if __name__ == '__main__':
    win32api.SetConsoleCtrlHandler(ctrlHandler, True)
    win32serviceutil.HandleCommandLine(DemoService, argv=sys.argv)
   
I use this script to run to download mails for different user_id and password
from configured mail server.

Generally i run this script as batch file which takes parameters like user_id
and password

python demo_svcs.py install
python demo_svcs.py start %user_id% %password%

At present in my code i am passing user_id and password as argv
 "win32serviceutil.HandleCommandLine(DemoService, argv=sys.argv)"
 which intern i am accessing these user_id and password in 'SvcDoRun' method
 with 'args', i just wanted to know is this the correct approach or do we have flexibility
 to overwrite 'SvcDoRun' method to get these 'user_id' and 'password' as kwd_args
 and use these kwd_args in 'start_mail_check' method.

I want to run the same service with different user_id and password as command line args
for multiple users.

I gooled to find out examples with with parameters, but i am not lucky enough to find out.

Could any one suggest me move forward in this regard.

Thanks,
Pavi
 









_______________________________________________
python-win32 mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-win32
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Wanted to know how we can overwrite the win32serviceutil.ServiceFramework SvcDoRun method.

Tim Roberts
pavi ena wrote:

>
> I am new to Python Window services development. I have small code below.
> ...
> Generally i run this script as batch file which takes parameters like
> user_id
> and password
>
> python demo_svcs.py install
> python demo_svcs.py start %user_id% %password%
>
> At present in my code i am passing user_id and password as argv
>  "win32serviceutil.HandleCommandLine(DemoService, argv=sys.argv)"
>  which intern i am accessing these user_id and password in 'SvcDoRun'
> method
>  with 'args', i just wanted to know is this the correct approach or do
> we have flexibility
>  to overwrite 'SvcDoRun' method to get these 'user_id' and 'password'
> as kwd_args
>  and use these kwd_args in 'start_mail_check' method.

Remember that you have the full source code to win32serviceutil.py in
your Python distribution.  You can go look it up.  SvcDoRun is called
from win32serviceutil.py.  You'll see that it is called from
ServiceFramework.SvcRun with no parameters:
    self.SvcDoRun()

SvcRun is called in response to a message from the Service Control
Manager, so there's really no place to make a modification like that.  I
think you need to keep doing what you are doing.

--
Tim Roberts, [hidden email]
Providenza & Boekelheide, Inc.

_______________________________________________
python-win32 mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-win32
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Wanted to know how we can overwrite the win32serviceutil.ServiceFramework SvcDoRun method.

pavi ena
Hello Tim Roberts,

Thanks a lot. I will open up the source code and try to understand the implementation. 
I will continue to work with my existing code.






On Thu, Mar 29, 2012 at 10:37 PM, Tim Roberts <[hidden email]> wrote:
pavi ena wrote:
>
> I am new to Python Window services development. I have small code below.
> ...
> Generally i run this script as batch file which takes parameters like
> user_id
> and password
>
> python demo_svcs.py install
> python demo_svcs.py start %user_id% %password%
>
> At present in my code i am passing user_id and password as argv
>  "win32serviceutil.HandleCommandLine(DemoService, argv=sys.argv)"
>  which intern i am accessing these user_id and password in 'SvcDoRun'
> method
>  with 'args', i just wanted to know is this the correct approach or do
> we have flexibility
>  to overwrite 'SvcDoRun' method to get these 'user_id' and 'password'
> as kwd_args
>  and use these kwd_args in 'start_mail_check' method.

Remember that you have the full source code to win32serviceutil.py in
your Python distribution.  You can go look it up.  SvcDoRun is called
from win32serviceutil.py.  You'll see that it is called from
ServiceFramework.SvcRun with no parameters:
   self.SvcDoRun()

SvcRun is called in response to a message from the Service Control
Manager, so there's really no place to make a modification like that.  I
think you need to keep doing what you are doing.

--
Tim Roberts, [hidden email]
Providenza & Boekelheide, Inc.

_______________________________________________
python-win32 mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-win32


_______________________________________________
python-win32 mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-win32
Loading...