Created
November 27, 2018 22:14
-
-
Save jorgepsmatos/9aa0fef750baccf6d6abe457b911afef to your computer and use it in GitHub Desktop.
Python Http Server as Windows Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from SMWinservice import SMWinservice | |
import SimpleHTTPServer, SocketServer | |
import urlparse, os | |
PORT = 8000 | |
INDEXFILE = 'index.html' | |
class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): | |
def do_GET(self): | |
# Parse query data to find out what was requested | |
parsedParams = urlparse.urlparse(self.path) | |
# See if the file requested exists | |
if os.access('.' + os.sep + parsedParams.path, os.R_OK): | |
# File exists, serve it up | |
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self); | |
else: | |
# send index.html, but don't redirect | |
self.send_response(200) | |
self.send_header('Content-Type', 'text/html') | |
self.end_headers() | |
with open(INDEXFILE, 'r') as fin: | |
self.copyfile(fin, self.wfile) | |
class PythonHttpServer(SMWinservice): | |
_svc_name_ = "PythonHttpServer" | |
_svc_display_name_ = "Angular Quiosk Http Server" | |
_svc_description_ = "Python http server hosting Angular Quiosk App" | |
def start(self): | |
self.isrunning = True | |
def stop(self): | |
self.isrunning = False | |
def main(self): | |
Handler = MyHandler | |
httpd = SocketServer.TCPServer(("", PORT), Handler) | |
httpd.serve_forever() | |
if __name__ == '__main__': | |
PythonHttpServer.parse_command_line() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
SMWinservice | |
by Davide Mastromatteo | |
from https://medium.com/the-python-corner/how-to-create-a-windows-service-in-python-88ca534ce76b | |
Base class to create winservice in Python | |
----------------------------------------- | |
Instructions: | |
1. Just create a new class that inherits from this base class | |
2. Define into the new class the variables | |
_svc_name_ = "nameOfWinservice" | |
_svc_display_name_ = "name of the Winservice that will be displayed in scm" | |
_svc_description_ = "description of the Winservice that will be displayed in scm" | |
3. Override the three main methods: | |
def start(self) : if you need to do something at the service initialization. | |
A good idea is to put here the inizialization of the running condition | |
def stop(self) : if you need to do something just before the service is stopped. | |
A good idea is to put here the invalidation of the running condition | |
def main(self) : your actual run loop. Just create a loop based on your running condition | |
4. Define the entry point of your module calling the method "parse_command_line" of the new class | |
5. Enjoy | |
''' | |
import socket | |
import win32serviceutil | |
import servicemanager | |
import win32event | |
import win32service | |
class SMWinservice(win32serviceutil.ServiceFramework): | |
'''Base class to create winservice in Python''' | |
_svc_name_ = 'pythonService' | |
_svc_display_name_ = 'Python Service' | |
_svc_description_ = 'Python Service Description' | |
@classmethod | |
def parse_command_line(cls): | |
''' | |
ClassMethod to parse the command line | |
''' | |
win32serviceutil.HandleCommandLine(cls) | |
def __init__(self, args): | |
''' | |
Constructor of the winservice | |
''' | |
win32serviceutil.ServiceFramework.__init__(self, args) | |
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) | |
socket.setdefaulttimeout(60) | |
def SvcStop(self): | |
''' | |
Called when the service is asked to stop | |
''' | |
self.stop() | |
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) | |
win32event.SetEvent(self.hWaitStop) | |
def SvcDoRun(self): | |
''' | |
Called when the service is asked to start | |
''' | |
self.start() | |
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, | |
servicemanager.PYS_SERVICE_STARTED, | |
(self._svc_name_, '')) | |
self.main() | |
def start(self): | |
''' | |
Override to add logic before the start | |
eg. running condition | |
''' | |
pass | |
def stop(self): | |
''' | |
Override to add logic before the stop | |
eg. invalidating running condition | |
''' | |
pass | |
def main(self): | |
''' | |
Main class to be ovverridden to add logic | |
''' | |
pass | |
# entry point of the module: copy and paste into the new module | |
# ensuring you are calling the "parse_command_line" of the new created class | |
if __name__ == '__main__': | |
SMWinservice.parse_command_line() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment