SystemRage / py-kms

KMS Server Emulator written in Python
The Unlicense
2.05k stars 628 forks source link

Run py-kms as windows 2008R2 service #2

Closed ghost closed 6 years ago

ghost commented 6 years ago

Download and install: https://www.python.org/ftp/python/2.7.14/python-2.7.14.amd64.msi (into C:\Windows\Python27) https://github.com/mhammond/pywin32/releases/download/b222/pywin32-222.win-amd64-py2.7.exe

Download and extract py-kms-master.zip into C:\Windows\Python27\py-kms cd C:\Windows\Python27\py-kms\

Create file kms-winservice.py and put into file this code:

import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
import subprocess

class AppServerSvc (win32serviceutil.ServiceFramework):
    _svc_name_ = "py-kms"
    _svc_display_name_ = "py-kms"
    _proc = None
    _cmd = ["C:\Windows\Python27\python.exe", "C:\Windows\Python27\py-kms\server.py"]

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

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

    def SvcDoRun(self):
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                              servicemanager.PYS_SERVICE_STARTED,
                              (self._svc_name_,''))
        self.main()

    def main(self):
        self._proc = subprocess.Popen(self._cmd)
        self._proc.wait()

    def killproc(self):
        self._proc.kill()

if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(AppServerSvc)

C:\Windows\Python27\python.exe kms-winservice.py install services.msc

Find "py-kms" service and change startup type from manual to auto. Try to start the py-kms service. Try activate Windows Server 2008 R2 VL: slmgr.vbs /skms 127.0.0.1 slmgr.vbs /ato

SystemRage commented 6 years ago

Approach that should go, so added to wiki. If fails, a more universal solution is to use Non-Sucking Service Manager.