How to run Python on windows service

Source: Internet
Author: User

Today we will introduce some application tips on running Python on windows service. I believe that anyone who has used the Python programming language will find that this powerful and easy-to-use programming language can help us easily implement many functional requirements.

 
 
  1. Import wmi
  2. Import OS
  3. C = wmi. WMI ()
  4. Watcher = c. Win32_PowerManagementEvent.watch_for
    (EventType = 7) # monitor the standby event;
  5. While True:
  6. OS. system ("kdlj. vbs") # Run the "connect to broadband" program,
    The vbs code of the old brother was used here;
  7. Watcher ()

Because the windows service has been running Python console windows, it seems a bit inconvenient. So I thought that if I could run it in windows service, it would be like other programs in windows service Manager.

Finally, I found the related introduction in the book "Python Programming On Win32" by Mark Hammond), which contains a simple template that puts the program code in the corresponding position:

 
 
  1. # SmallestService. py
  2. #
  3. # A sample demonstrating the smallest possible service written in Python.
  4. Import win32serviceutil
  5. Import win32service
  6. Import win32event
  7. Class SmallestPythonService (win32serviceutil. ServiceFramework ):
  8. _ Svc_name _ = "SmallestPythonService"
  9. _ Svc_display_name _ = "The smallest possible Python Service"
  10. Def _ init _ (self, args ):
  11. Win32serviceutil. ServiceFramework. _ init _ (self, args)
  12. # Create an event which we will use to wait on.
  13. # The "service stop" request will set this event.
  14. Self. hWaitStop = win32event. CreateEvent (None, 0, 0, None)
  15. Def SvcStop (self ):
  16. # Before we do anything, tell the SCM we are starting the stop process.
  17. Self. ReportServiceStatus (win32service. SERVICE_STOP_PENDING)
  18. # And set my event.
  19. Win32event. SetEvent (self. hWaitStop)
  20. Def SvcDoRun (self ):
  21. # Put your program code here.
  22. Win32event. WaitForSingleObject (self. hWaitStop, win32event. INFINITE)
  23. If _ name __= = '_ main __':
  24. Win32serviceutil. HandleCommandLine (SmallestPythonService)
  25. # The name in the brackets can be changed to another name, which must be the same as the class name;

Next, just install the service and run SmallestService. py install in cmd. In this way, you can find a Service named "The smallest possible Python Service" in The windows Service manager. If it is set to automatic start, it will automatically start and run in The background. ,)

However, even if windows Firewall is enabled.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.