Python Win32serviceutil Install Service

The you have three options through command prompt where you can install, start, stop the service. C: python start.py install (This install the commtelsms.py as a service) C: python start.py start (This start the commtelsms.py as a service) C: python start.py stop (This stop the commtelsms.py as a service) Download. If I remember correctly, Python's import mechanism causes the thread where the import takes place to deadlock, so you should always perform an import inside your main thread. But a little Python can easily do the trick. First you need to install the Python for Windows extension, which adds a lot of neat functions, including what we’re looking for - win32serviceutil. Now we build a simple function to stop, start, restart, or query the status of a Windows service. Now we’re off and running. The functions this recipe uses from the win32serviceutil module are StartService, StopService, RestartService, and QueryServiceStatus. Each takes two arguments: the name of the service and the name of the machine. The first three perform the start, stop, and restart as requested. The fourth returns a structured code describing whether and how.

What follows are some guides how to start the pykms_Server.py script, which provides the emulated server.

Running as a service¶

You can simply manage a daemon that runs as a background process. This can be achieved by using any of the notes below or by writing your own solution.

Docker¶

If you wish to get py-kms just up and running without installing any dependencies or writing own scripts: Just use Docker !Docker also solves problems regarding the explicit IPv4 and IPv6 usage (it just supports both). The followingcommand will download, “install” and start py-kms and also keep it alive after any service disruption.

If you just want to use the image and don’t want to build them yourself, you can always use the official image at the Docker Hub (pykmsorg/py-kms). To ensure that you are using always thelatest version you should check something like watchtower out !

Tags¶

There are currently three tags of the image available (select one just by appending :<tag> to the image from above):

  • latest, currently the same like minimal.

  • minimal, which is based on the python3 minimal configuration of py-kms. This tag does NOT include sqlite support !

  • python3, which is fully configurable and equipped with sqlite support and a web interface (make sure to expose port 8080) for management.

Python Win32serviceutil Install Service

Architectures¶

There are currently the following architectures available (if you need an other, feel free to open an issue):

  • amd64

  • arm32v6 Raspberry PI 1 (A, A+, B, B+, Zero)

  • arm32v7 Raspberry PI 2 (B)

  • arm64v8 Raspberry PI 2 (B v1.2), Raspberry PI 3 (A+, B, B+), Raspberry PI 4 (B)

Please note that any architecture other than the classic amd64 is slightly bigger (~4 MB), caused by the use of qemu during building.

Docker Compose¶

You can use docker-compose instead of building and running the Dockerfile, so you do not need to respecify your settings again and again. The following Docker Compose file will deploy the latest image with the log into your local directory.

Parameters¶

Below is a fully expanded run command, detailing all the different supported environment variables to set. For further reference see the start parameters for the docker environment.

You can omit the -eSQLITE=... and -p8080:8080 option if you plan to use the minimal or latest image, which does not include the respective module support.

Systemd¶

If you are running a Linux distro using systemd, create the file: sudonano/etc/systemd/system/py3-kms.service, then add the following (change it where needed) and save:

Check syntax with sudosystemd-analyzeverifypy3-kms.service, correct file permission (if needed) sudochmod644/etc/systemd/system/py3-kms.service, then reload systemd manager configuration sudosystemctldaemon-reload,start the daemon sudosystemctlstartpy3-kms.service and view its status sudosystemctlstatuspy3-kms.service. Check if daemon is correctly running with cat</path/to/your/log/files/folder>/pykms_logserver.log. Finally afew generic commands useful for interact with your daemon here.

Etrigan¶

You can run py-kms daemonized (via Etrigan) using a command like python3pykms_Server.pyetriganstart and stop it with python3pykms_Server.pyetriganstop. With Etrigan you have anotherway to launch py-kms GUI (specially suitable if you’re using a virtualenv), so python3pykms_Server.pyetriganstart-g and stop the GUI with python3pykms_Server.pyetriganstop (or interact with the EXIT button).

Upstart (deprecated)¶

If you are running a Linux distro using upstart (deprecated), create the file: sudonano/etc/init/py3-kms.conf, then add the following (change it where needed) and save:

Check syntax with sudoinit-checkconf-d/etc/init/py3-kms.conf, then reload upstart to recognise this process sudoinitctlreload-configuration. Now start the service sudostartpy3-kms, and you can see the logfilestating that your daemon is running: cat</path/to/your/log/files/folder>/pykms_logserver.log. Finally a few generic commands useful for interact with your daemon here.

Windows¶

If you are using Windows, to run pykms_Server.py as service you need to install pywin32, then you can create a file for example named kms-winservice.py and put into it this code:

Now in a command prompt type C:WindowsPython27python.exekms-winservice.pyinstall to install the service. Display all the services with services.msc and find the service associated with py-kms, change the startup typefrom manual to auto. Finally Start the service. If this approach fails, you can try to use Non-Sucking Service Manager or Task Scheduler as described here.

Other Platforms¶

They might be useful to you:

Python

Manual Execution¶

Dependencies¶

  • Python 3.x.

  • Tkinter module (for the GUI).

  • If the tzlocal module is installed, the “Request Time” in the verbose output will be converted into local time. Otherwise, it will be in UTC.

  • It can use the sqlite3 module, storing activation data in a database so it can be recalled again.

  • Installation example on Ubuntu / Mint:

    • sudoapt-getupdate

    • sudoapt-getinstallpython3-tkpython3-pip

    • sudopip3installtzlocalpysqlite3

Startup¶

A Linux user with ipaddr command can get his KMS IP (Windows users can try ipconfig/all).

In the example above is 192.168.1.102 the ip we want to listen on, so it is this command (note you can omit the ip AND port specification if you just wish to listen on all interfaces with port 1688):

To stop pykms_Server.py, in the same bash window where code running, simply press CTRL+C.Alternatively, in a new bash window, use kill<pid> command (you can type psaux first and have the process ) or killall<name_of_server>.

Quick Guide¶

The following are just some brief notes about parameters handling. For a more detailed description see here.

  • To generate a random HWID use -w option: python3pykms_Server.py-wRANDOM.

  • To get the HWID from any server use the client, for example type: python3pykms_Client.py::1688-mWindows8.1-VINFO.

  • To change your logfile path use -F option, for example: python3pykms_Server.py-F/path/to/your/logfile.log-VDEBUG.

  • To view a minimal set of logging information use -VMININFO option, for example: python3pykms_Server.py-F/path/to/your/logfile.log-VMININFO.

  • To redirect logging on stdout use -FSTDOUT option, for example: python3pykms_Server.py-FSTDOUT-VDEBUG.

  • You can create logfile and view logging information on stdout at the same time with -FFILESTDOUT option, for example: python3pykms_Server.py-FFILESTDOUT/path/to/your/logfile.log-VDEBUG.

  • With -FSTDOUTOFF you disable all stdout messages (but a logfile will be created), for example: python3pykms_Server.py-FSTDOUTOFF/path/to/your/logfile.log-VDEBUG.

  • With -FFILEOFF you disable logfile creation.

  • Select timeout (seconds) for py-kms with -t0 option, for example python3pykms_Server.py-t010.

  • Option -y enables printing asynchronously of messages (pretty / logging).

The advantage of the program is going to run on system boot.

So basically a program or software that has to run non stop without any interruption.

import win32service
import win32serviceutil
Python Win32serviceutil Install Service

Python Win32serviceutil Install Service

import win32api

Python Win32serviceutil Install Service Pack

import win32con
import win32event
import win32evtlogutil
import os, sys, string, time

Python Win32serviceutil Install Service Panel

class aservice(win32serviceutil.ServiceFramework):

_svc_name_ = “PIDS Alarms”
_svc_display_name_ = “Commtel SMS”
_svc_description_ = “Sending SMS via GSM dongle to send out alert!”

def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)

def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)

def SvcDoRun(self):
import servicemanager
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,servicemanager.PYS_SERVICE_STARTED,(self._svc_name_, ”))

#self.timeout = 640000 #640 seconds / 10 minutes (value is in milliseconds)
self.timeout = 120000 #120 seconds / 2 minutes
# This is how long the service will wait to run / refresh itself (see script below)

while 1:
# Wait for service stop signal, if I timeout, loop again
rc = win32event.WaitForSingleObject(self.hWaitStop, self.timeout)
# Check to see if self.hWaitStop happened
if rc win32event.WAIT_OBJECT_0:
# Stop signal encountered
servicemanager.LogInfoMsg(“SomeShortNameVersion – STOPPED!”) #For Event Log
break
else:
#Ok, here’s the real money shot right here.
#[actual service code between rests]
try:
file_path = r”C:yourfile.py”
execfile(file_path) #Execute the script
inc_file_path2 = r”C:PIDScommtel_txt.py”
execfile(inc_file_path2) #Execute the script
except:
pass
#[actual service code between rests]

def ctrlHandler(ctrlType):
return True

if __name__ ‘__main__’:
win32api.SetConsoleCtrlHandler(ctrlHandler, True)
win32serviceutil.HandleCommandLine(aservice)

Save the above code in start.py

Installutil

The you have three options through command prompt where you can install, start, stop the service

Python Win32serviceutil

C:>python start.py install (This install the commtel_sms.py as a service)
C:>python start.py start (This start the commtel_sms.py as a service)
C:>python start.py stop (This stop the commtel_sms.py as a service)