This repository has been archived by the owner on Apr 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmapproxy_srv.py
executable file
·82 lines (70 loc) · 2.87 KB
/
mapproxy_srv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""
The most basic (working) CherryPy 3.2 WSGI Windows service possible.
Requires Mark Hammond's pywin32 package.
Taken from here: http://tools.cherrypy.org/wiki/WindowsService and modified.
License and copyright unknown. No licence or warranty claimed on my behalf.
Inquire with source mentioned above regarding either.
To see a list of options for installing, removing, starting, and stopping your service.
python this_file.py
To install your new service type:
python this_file.py install
Then type:
python this_file.py start
If you get "Access Denied" you need to be admin to install, remove, start, stop.
( To run cmd as admin: Windows Key > "cmd" > CTRL+SHIFT+ENTER )
"""
import pywintypes, pythoncom, win32api, win32serviceutil, win32service
from logging.config import fileConfig
import os.path
import sys
import cherrypy
from cherrypy import wsgiserver
from mapproxy.wsgiapp import make_wsgi_app
if sys.hexversion > 0x03000000:
import winreg
else:
import _winreg as winreg
# globals
version='1.0.0'
rootkey=winreg.HKEY_LOCAL_MACHINE
subkey=r'SOFTWARE\COMPANY\APP\NUM'
server_ip='0.0.0.0'
class MyService(win32serviceutil.ServiceFramework):
"""NT Service."""
_svc_name_ = 'MapProxy-' + version
_svc_display_name_ = 'MapProxy ' + version
_svc_description_ = 'This service runs the MapProxy tile server'
def SvcDoRun(self):
key=winreg.OpenKey(rootkey, subkey, 0, winreg.KEY_READ)
port_to_bind=int(winreg.QueryValueEx(key, 'Port')[0])
data_dir=str(winreg.QueryValueEx(key, 'DataDir')[0])
app_config=data_dir + r'\mapproxy.yaml'
log_conf=data_dir + r'\log.ini'
cherrypy.config.update({
'global':{
'log.screen': False,
'tools.log_tracebacks.on': True,
'engine.autoreload.on': False,
'engine.SIGHUP': None,
'engine.SIGTERM': None
}
})
fileConfig(log_conf, {'here': data_dir})
application=make_wsgi_app(app_config)
d=wsgiserver.WSGIPathInfoDispatcher({'/mapproxy': application})
self.server=wsgiserver.CherryPyWSGIServer( (server_ip, port_to_bind), d, numthreads=10, server_name=None, max=-1, request_queue_size=2048, timeout=10, shutdown_timeout=5)
# Infinite loop serving requests
try:
self.server.start()
except Exception as e:
# Log an error event
servicemanager.LogErrorMsg("MapProxy failed to start:\n%s" % e)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
if self.server:
self.server.stop()
self.ReportServiceStatus(win32service.SERVICE_STOPPED)
# very important for use with py2exe
# otherwise the Service Controller never knows that it is stopped !
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(MyService)